I'm using Linux and have installed Python 3.9.1 and 3.8.5 (in different environments). I'm trying to follow the documentation for the multiprocessing
module. However, none of the examples works.
The first example is:
from multiprocessing import Pool
def f(x):
return x*x
if __name__ == '__main__':
with Pool(5) as p:
print(p.map(f, [1, 2, 3]))
I created a file containing only that code and ran it. The result is:
Traceback (most recent call last):
File "multiprocessing.py", line 1, in <module>
from multiprocessing import Pool
File "/home/helitonmrf/Documents/TEMP/multiprocessing.py", line 1, in <module>
from multiprocessing import Pool
ImportError: cannot import name 'Pool' from partially initialized module 'multiprocessing' (most likely due to a circular import) (/home/helitonmrf/Documents/TEMP/multiprocessing.py)
Trying the context part, with the following code:
import multiprocessing as mp
def foo(q):
q.put('hello')
if __name__ == '__main__':
mp.set_start_method('spawn')
q = mp.Queue()
p = mp.Process(target=foo, args=(q,))
p.start()
print(q.get())
p.join()
Also doesn't work and I get:
Traceback (most recent call last):
File "multiprocessing.py", line 9, in <module>
mp.set_start_method('spawn')
AttributeError: module 'multiprocessing' has no attribute 'set_start_method'
What's going on?
The problem you are having is because you named your file multiprocessing.py
, which is also the name of the library you are trying to use.
Python looks in the current directory first when it looks to import libraries. Rename your file (and be sure to delete multiprocessing.pyc
if it exists).