diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2011-04-10 22:18:59 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2011-04-10 22:18:59 (GMT) |
commit | 81dee6b4d4acc061e568e10f976eb77b084ddd26 (patch) | |
tree | 12c351bd842def366f23a327ef6589409f3f05f1 /Lib/multiprocessing/pool.py | |
parent | 810b94a36416cecf38205fd48e6a0a9eb3d2998c (diff) | |
download | cpython-81dee6b4d4acc061e568e10f976eb77b084ddd26.zip cpython-81dee6b4d4acc061e568e10f976eb77b084ddd26.tar.gz cpython-81dee6b4d4acc061e568e10f976eb77b084ddd26.tar.bz2 |
Issue #8428: Fix a race condition in multiprocessing.Pool when terminating
worker processes: new processes would be spawned while the pool is being
shut down. Patch by Charles-François Natali.
Diffstat (limited to 'Lib/multiprocessing/pool.py')
-rw-r--r-- | Lib/multiprocessing/pool.py | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/Lib/multiprocessing/pool.py b/Lib/multiprocessing/pool.py index ff7c29c..efa0056 100644 --- a/Lib/multiprocessing/pool.py +++ b/Lib/multiprocessing/pool.py @@ -322,6 +322,8 @@ class Pool(object): while pool._worker_handler._state == RUN and pool._state == RUN: pool._maintain_pool() time.sleep(0.1) + # send sentinel to stop workers + pool._taskqueue.put(None) debug('worker handler exiting') @staticmethod @@ -440,7 +442,6 @@ class Pool(object): if self._state == RUN: self._state = CLOSE self._worker_handler._state = CLOSE - self._taskqueue.put(None) def terminate(self): debug('terminating pool') @@ -474,7 +475,6 @@ class Pool(object): worker_handler._state = TERMINATE task_handler._state = TERMINATE - taskqueue.put(None) # sentinel debug('helping task handler/workers to finish') cls._help_stuff_finish(inqueue, task_handler, len(pool)) @@ -484,6 +484,11 @@ class Pool(object): result_handler._state = TERMINATE outqueue.put(None) # sentinel + # We must wait for the worker handler to exit before terminating + # workers because we don't want workers to be restarted behind our back. + debug('joining worker handler') + worker_handler.join() + # Terminate workers which haven't already finished. if pool and hasattr(pool[0], 'terminate'): debug('terminating workers') |