You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
multiprocessing.Pool from concurrent threads failure on 3.9.0rc1 #85739
If several threads try to start a multiprocessing.Pool at the same time when no pool has been started before this often fails with an exception like this (the exact import varies):
Exception in thread Thread-2:
Traceback (most recent call last):
File "/tmp/py3.9.0rc1/lib/python3.9/threading.py", line 950, in _bootstrap_innerself.run()
File "/tmp/py3.9.0rc1/lib/python3.9/threading.py", line 888, in runself._target(*self._args, **self._kwargs)
File "/tmp/py3.9.0rc1/lib/python3.9/multiprocessing/context.py", line 118, in Poolfrom .pool import Pool
ImportError: cannot import name 'Pool' from partially initialized module 'multiprocessing.pool' (most likely due to a circular import) (/tmp/py3.9.0rc1/lib/python3.9/multiprocessing/pool.py)
This happens even if Pool was imported before starting the threads and is new in 3.9. It's easy to work around by starting a pool in the main thread before starting the other threads.
I have attached a minimal example that triggers it. Tested on Debian stable and FreeBSD 11.3.
If I change the relative imports to absolute imports in a couple of functions in multiprocessing.context as below, the attached (pool_error_on_3.9.py) script not longer raises the exception.
defSimpleQueue(self):
'''Returns a queue object'''frommultiprocessing.queuesimportSimpleQueuereturnSimpleQueue(ctx=self.get_context())
defPool(self, processes=None, initializer=None, initargs=(),
maxtasksperchild=None):
'''Returns a process pool object'''frommultiprocessing.poolimportPoolreturnPool(processes, initializer, initargs, maxtasksperchild,
context=self.get_context())
Activity
drougge commentedon Aug 17, 2020
If several threads try to start a multiprocessing.Pool at the same time when no pool has been started before this often fails with an exception like this (the exact import varies):
This happens even if Pool was imported before starting the threads and is new in 3.9. It's easy to work around by starting a pool in the main thread before starting the other threads.
I have attached a minimal example that triggers it. Tested on Debian stable and FreeBSD 11.3.
iritkatriel commentedon Aug 17, 2020
I see the same in Python 3.10 on windows 10.
If I change the relative imports to absolute imports in a couple of functions in multiprocessing.context as below, the attached (pool_error_on_3.9.py) script not longer raises the exception.
iritkatriel commentedon Aug 17, 2020
I think this is no a bug, on the basis that multiprocessing.Pool is not thread-safe.
atsuoishimoto commentedon Oct 23, 2020
multiprocessing module used to work multithread environment until https://bugs.python.org/issue35943 merged.
I guess we can make multiprocessing thread-safe again if we move local imports to global.
Does it make sense?
atsuoishimoto commentedon Oct 23, 2020
Some my observation at https://discuss.python.org/t/differences-between-3-8-and-3-9-in-importing-module/5520 .
doublex commentedon Mar 15, 2021
Example code (fails):