Skip to content

Commit b30d4ed

Browse files
committed
CLN address review comments
1 parent c3274e4 commit b30d4ed

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

Lib/concurrent/futures/process.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import os
5050
from concurrent.futures import _base
5151
import queue
52-
from queue import Full
5352
import multiprocessing as mp
5453
import multiprocessing.connection
5554
from multiprocessing.queues import Queue
@@ -272,7 +271,7 @@ class _ExecutorManagerThread(threading.Thread):
272271

273272
def __init__(self, executor):
274273

275-
# When the executor gets garbarge collected, the weakref callback
274+
# When the executor gets garbage collected, the weakref callback
276275
# will wake up the queue management thread so that it can terminate
277276
# if there is no pending work item.
278277
self.thread_wakeup = executor._executor_manager_thread_wakeup
@@ -312,9 +311,14 @@ def run(self):
312311
# while waiting on new results.
313312
del result_item
314313

315-
if (self.is_shutting_down()
316-
and self.shutdown_executor_when_no_pending_tasks()):
317-
return
314+
if self.is_shutting_down():
315+
self.flag_executor_shutting_down()
316+
317+
# Since no new work items can be added, it is safe to shutdown
318+
# this thread if there are no pending work items.
319+
if not self.pending_work_items:
320+
self.join_executor_internals()
321+
return
318322

319323
def add_call_item_to_queue(self):
320324
# Fills call_queue with _WorkItems from pending_work_items.
@@ -363,7 +367,6 @@ def wait_result_broken_or_wakeup(self):
363367

364368
elif wakeup_reader in ready:
365369
is_broken = False
366-
result_item = None
367370
self.thread_wakeup.clear()
368371

369372
return result_item, is_broken, cause
@@ -379,7 +382,7 @@ def process_result_item(self, result_item):
379382
p = self.processes.pop(result_item)
380383
p.join()
381384
if not self.processes:
382-
self.shutdown_worker()
385+
self.join_executor_internals()
383386
return
384387
else:
385388
# Received a _ResultItem so mark the future as completed.
@@ -402,9 +405,9 @@ def is_shutting_down(self):
402405
or executor._shutdown_thread)
403406

404407
def terminate_broken(self, cause):
405-
# Terminate the executor because it is in broken state. The cause
408+
# Terminate the executor because it is in a broken state. The cause
406409
# argument can be used to display more information on the error that
407-
# leaded the executor to be broken.
410+
# lead the executor into becoming broken.
408411

409412
# Mark the process pool broken so that submits fail right now.
410413
executor = self.executor_reference()
@@ -436,10 +439,10 @@ def terminate_broken(self, cause):
436439
for p in self.processes.values():
437440
p.terminate()
438441

439-
# clean up ressources
440-
self.shutdown_worker()
442+
# clean up resources
443+
self.join_executor_internals()
441444

442-
def shutdown_executor_when_no_pending_tasks(self):
445+
def flag_executor_shutting_down(self):
443446
# Flag the executor as shutting down and cancel remaining tasks if
444447
# requested as early as possible if it is not gc-ed yet.
445448
executor = self.executor_reference()
@@ -465,13 +468,7 @@ def shutdown_executor_when_no_pending_tasks(self):
465468
# on running processes over and over.
466469
executor._cancel_pending_futures = False
467470

468-
# Since no new work items can be added, it is safe to shutdown
469-
# this thread if there are no pending work items.
470-
if not self.pending_work_items:
471-
self.shutdown_worker()
472-
return True
473-
474-
def shutdown_worker(self):
471+
def shutdown_workers(self):
475472
n_children_to_stop = self.get_n_children_alive()
476473
n_sentinels_sent = 0
477474
# Send the right number of sentinels, to make sure all children are
@@ -482,9 +479,11 @@ def shutdown_worker(self):
482479
try:
483480
self.call_queue.put_nowait(None)
484481
n_sentinels_sent += 1
485-
except Full:
482+
except queue.Full:
486483
break
487484

485+
def join_executor_internals(self):
486+
self.shutdown_workers()
488487
# Release the queue's resources as soon as possible.
489488
self.call_queue.close()
490489
self.call_queue.join_thread()

0 commit comments

Comments
 (0)