Skip to content

Commit 8010cef

Browse files
miss-islingtonjackcvrblurb-it[bot]hauntsaninja
authored
[3.12] gh-102024: Reduced _idle_semaphore.release calls (GH-102025) (#104959)
gh-102024: Reduced _idle_semaphore.release calls (GH-102025) Reduced _idle_semaphore.release calls in concurrent.futures.thread._worker _idle_semaphore.release() is now only called if only work_queue is empty. --------- (cherry picked from commit 0242e9a) Co-authored-by: Andrii Kuzmin <[email protected]> Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com> Co-authored-by: Shantanu <[email protected]>
1 parent 5c2971b commit 8010cef

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

Lib/concurrent/futures/thread.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def _python_exit():
4343
after_in_parent=_global_shutdown_lock.release)
4444

4545

46-
class _WorkItem(object):
46+
class _WorkItem:
4747
def __init__(self, future, fn, args, kwargs):
4848
self.future = future
4949
self.fn = fn
@@ -78,17 +78,20 @@ def _worker(executor_reference, work_queue, initializer, initargs):
7878
return
7979
try:
8080
while True:
81-
work_item = work_queue.get(block=True)
82-
if work_item is not None:
83-
work_item.run()
84-
# Delete references to object. See issue16284
85-
del work_item
86-
87-
# attempt to increment idle count
81+
try:
82+
work_item = work_queue.get_nowait()
83+
except queue.Empty:
84+
# attempt to increment idle count if queue is empty
8885
executor = executor_reference()
8986
if executor is not None:
9087
executor._idle_semaphore.release()
9188
del executor
89+
work_item = work_queue.get(block=True)
90+
91+
if work_item is not None:
92+
work_item.run()
93+
# Delete references to object. See GH-60488
94+
del work_item
9295
continue
9396

9497
executor = executor_reference()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Reduce calls of ``_idle_semaphore.release()`` in :func:`concurrent.futures.thread._worker`.

0 commit comments

Comments
 (0)