diff options
author | Andrii Kuzmin <jack.cvr@gmail.com> | 2023-05-26 05:48:40 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-26 05:48:40 (GMT) |
commit | 0242e9a57aa87ed0b5cac526f65631c654a39054 (patch) | |
tree | caec5b07e00521b9e0789bd46f44a8ac1c0485ff /Lib/concurrent | |
parent | 72c3d2e105f120f6f2bce410699b34dac4e948fd (diff) | |
download | cpython-0242e9a57aa87ed0b5cac526f65631c654a39054.zip cpython-0242e9a57aa87ed0b5cac526f65631c654a39054.tar.gz cpython-0242e9a57aa87ed0b5cac526f65631c654a39054.tar.bz2 |
gh-102024: Reduced _idle_semaphore.release calls (#102025)
Reduced _idle_semaphore.release calls in concurrent.futures.thread._worker
_idle_semaphore.release() is now only called if only work_queue is empty.
---------
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
Diffstat (limited to 'Lib/concurrent')
-rw-r--r-- | Lib/concurrent/futures/thread.py | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/Lib/concurrent/futures/thread.py b/Lib/concurrent/futures/thread.py index 51c942f..3b3a36a 100644 --- a/Lib/concurrent/futures/thread.py +++ b/Lib/concurrent/futures/thread.py @@ -43,7 +43,7 @@ if hasattr(os, 'register_at_fork'): after_in_parent=_global_shutdown_lock.release) -class _WorkItem(object): +class _WorkItem: def __init__(self, future, fn, args, kwargs): self.future = future self.fn = fn @@ -78,17 +78,20 @@ def _worker(executor_reference, work_queue, initializer, initargs): return try: while True: - work_item = work_queue.get(block=True) - if work_item is not None: - work_item.run() - # Delete references to object. See issue16284 - del work_item - - # attempt to increment idle count + try: + work_item = work_queue.get_nowait() + except queue.Empty: + # attempt to increment idle count if queue is empty executor = executor_reference() if executor is not None: executor._idle_semaphore.release() del executor + work_item = work_queue.get(block=True) + + if work_item is not None: + work_item.run() + # Delete references to object. See GH-60488 + del work_item continue executor = executor_reference() |