Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3fdd9b6

Browse files
committed
Issue #11815: Remove dead code in concurrent.futures (since a blocking Queue
cannot raise queue.Empty).
2 parents 4bbde72 + 27be5da commit 3fdd9b6

2 files changed

Lines changed: 29 additions & 52 deletions

File tree

Lib/concurrent/futures/process.py

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def __init__(self, work_id, fn, args, kwargs):
104104
self.args = args
105105
self.kwargs = kwargs
106106

107-
def _process_worker(call_queue, result_queue, shutdown):
107+
def _process_worker(call_queue, result_queue):
108108
"""Evaluates calls from call_queue and places the results in result_queue.
109109
110110
This worker is run in a separate process.
@@ -118,24 +118,19 @@ def _process_worker(call_queue, result_queue, shutdown):
118118
worker that it should exit when call_queue is empty.
119119
"""
120120
while True:
121+
call_item = call_queue.get(block=True)
122+
if call_item is None:
123+
# Wake up queue management thread
124+
result_queue.put(None)
125+
return
121126
try:
122-
call_item = call_queue.get(block=True)
123-
except queue.Empty:
124-
if shutdown.is_set():
125-
return
127+
r = call_item.fn(*call_item.args, **call_item.kwargs)
128+
except BaseException as e:
129+
result_queue.put(_ResultItem(call_item.work_id,
130+
exception=e))
126131
else:
127-
if call_item is None:
128-
# Wake up queue management thread
129-
result_queue.put(None)
130-
return
131-
try:
132-
r = call_item.fn(*call_item.args, **call_item.kwargs)
133-
except BaseException as e:
134-
result_queue.put(_ResultItem(call_item.work_id,
135-
exception=e))
136-
else:
137-
result_queue.put(_ResultItem(call_item.work_id,
138-
result=r))
132+
result_queue.put(_ResultItem(call_item.work_id,
133+
result=r))
139134

140135
def _add_call_item_to_queue(pending_work_items,
141136
work_ids,
@@ -179,8 +174,7 @@ def _queue_manangement_worker(executor_reference,
179174
pending_work_items,
180175
work_ids_queue,
181176
call_queue,
182-
result_queue,
183-
shutdown_process_event):
177+
result_queue):
184178
"""Manages the communication between this process and the worker processes.
185179
186180
This function is run in a local thread.
@@ -198,9 +192,6 @@ def _queue_manangement_worker(executor_reference,
198192
derived from _WorkItems for processing by the process workers.
199193
result_queue: A multiprocessing.Queue of _ResultItems generated by the
200194
process workers.
201-
shutdown_process_event: A multiprocessing.Event used to signal the
202-
process workers that they should exit when their work queue is
203-
empty.
204195
"""
205196
nb_shutdown_processes = 0
206197
def shutdown_one_process():
@@ -213,20 +204,16 @@ def shutdown_one_process():
213204
work_ids_queue,
214205
call_queue)
215206

216-
try:
217-
result_item = result_queue.get(block=True)
218-
except queue.Empty:
219-
pass
220-
else:
221-
if result_item is not None:
222-
work_item = pending_work_items[result_item.work_id]
223-
del pending_work_items[result_item.work_id]
224-
225-
if result_item.exception:
226-
work_item.future.set_exception(result_item.exception)
227-
else:
228-
work_item.future.set_result(result_item.result)
229-
continue
207+
result_item = result_queue.get(block=True)
208+
if result_item is not None:
209+
work_item = pending_work_items[result_item.work_id]
210+
del pending_work_items[result_item.work_id]
211+
212+
if result_item.exception:
213+
work_item.future.set_exception(result_item.exception)
214+
else:
215+
work_item.future.set_result(result_item.result)
216+
continue
230217
# If we come here, we either got a timeout or were explicitly woken up.
231218
# In either case, check whether we should start shutting down.
232219
executor = executor_reference()
@@ -238,8 +225,6 @@ def shutdown_one_process():
238225
# Since no new work items can be added, it is safe to shutdown
239226
# this thread if there are no pending work items.
240227
if not pending_work_items:
241-
shutdown_process_event.set()
242-
243228
while nb_shutdown_processes < len(processes):
244229
shutdown_one_process()
245230
# If .join() is not called on the created processes then
@@ -306,7 +291,6 @@ def __init__(self, max_workers=None):
306291

307292
# Shutdown is a two-step process.
308293
self._shutdown_thread = False
309-
self._shutdown_process_event = multiprocessing.Event()
310294
self._shutdown_lock = threading.Lock()
311295
self._queue_count = 0
312296
self._pending_work_items = {}
@@ -324,8 +308,7 @@ def weakref_cb(_, q=self._result_queue):
324308
self._pending_work_items,
325309
self._work_ids,
326310
self._call_queue,
327-
self._result_queue,
328-
self._shutdown_process_event))
311+
self._result_queue))
329312
self._queue_management_thread.daemon = True
330313
self._queue_management_thread.start()
331314
_threads_queues[self._queue_management_thread] = self._result_queue
@@ -335,8 +318,7 @@ def _adjust_process_count(self):
335318
p = multiprocessing.Process(
336319
target=_process_worker,
337320
args=(self._call_queue,
338-
self._result_queue,
339-
self._shutdown_process_event))
321+
self._result_queue))
340322
p.start()
341323
self._processes.add(p)
342324

@@ -372,7 +354,6 @@ def shutdown(self, wait=True):
372354
self._queue_management_thread = None
373355
self._call_queue = None
374356
self._result_queue = None
375-
self._shutdown_process_event = None
376357
self._processes = None
377358
shutdown.__doc__ = _base.Executor.shutdown.__doc__
378359

Lib/concurrent/futures/thread.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,10 @@ def run(self):
6060
def _worker(executor_reference, work_queue):
6161
try:
6262
while True:
63-
try:
64-
work_item = work_queue.get(block=True)
65-
except queue.Empty:
66-
pass
67-
else:
68-
if work_item is not None:
69-
work_item.run()
70-
continue
63+
work_item = work_queue.get(block=True)
64+
if work_item is not None:
65+
work_item.run()
66+
continue
7167
executor = executor_reference()
7268
# Exit if:
7369
# - The interpreter is shutting down OR

0 commit comments

Comments
 (0)