Python multiprocessing (Pool) error

0

I have a problem with multiprocessing my program. This is the code of the program and below I insert the error:

Program:

import argparse
import sys
import multiprocessing
from multiprocessing import Pool
import time

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--x", type=float, default=1.0)
    args = parser.parse_args()
    sys.stdout.write(str(numeroprimo()))

num = input("Inserte un numero: ")


def numeroprimo():
    if int(num) > 1:
        for i in range(2, int(num)):
            if (int(num) % i) == 0:
                print(num, "no es un numero primo")
                break
        else:
            print(num, "es un numero primo")
    else:
        print(num, "no es un numero primo")


if __name__ == "__main__":
    main()
    t1 = time.time()
    for x in range(1):
        p = Pool()
        p.map(numeroprimo(), num)
        p.close()
        p.join()
    print("Tomó:", time.time()-t1)

Error:

multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
  File     "C:\Users\santi\AppData\Local\Programs\Python\Python35\lib\multiprocessing\pool.    py", line 119, in worker
    result = (True, func(*args, **kwds))
  File     "C:\Users\santi\AppData\Local\Programs\Python\Python35\lib\multiprocessing\pool.    py", line 44, in mapstar
    return list(map(*args))
TypeError: 'NoneType' object is not callable
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "Prime number checker.py", line 33, in <module>
    p.map(primenumber(), num)
  File     "C:\Users\santi\AppData\Local\Programs\Python\Python35\lib\multiprocessing\pool.    py", line 260, in map
    return self._map_async(func, iterable, mapstar, chunksize).get()
  File     "C:\Users\santi\AppData\Local\Programs\Python\Python35\lib\multiprocessing\pool.    py", line 608, in get
    raise self._value
TypeError: 'NoneType' object is not callable
    
asked by Santiago Pardal 24.01.2017 в 22:12
source

1 answer

1

On the line:

p.map(numeroprimo(), num)

On the one hand, with the parentheses () you are using the value returned by the function numeroprimo , whose default value is None for not explicitly indicating anything by a return . That is the error it gives you.

On the other hand, the function does not use arguments, so it is useless to pass the variable num . You'll be lucky to be caught for being a global variable.

But in the case that numeroprimo accepts arguments, Pool.map you have to pass an iterator as a second argument, something like this: [num]

Reinterpreting what you want to do, the code could be something like this:

def numeroprimo(n):
    res = n > 1 and all(n%i != 0 for i in range(2,n))
    print("{:6d}".format(n), "sí" if res else "no", "es número primo")
    return res

if __name__ == "__main__":
    #main()
    num = input("Inserte un numero: ")
    t1 = time.time()
    with Pool() as p:
        p.map(numeroprimo, range(2, int(num)))
    print("Tomó:", time.time()-t1)
    
answered by 25.01.2017 в 02:50