]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-102024: Reduced _idle_semaphore.release calls (#102025)
authorAndrii Kuzmin <jack.cvr@gmail.com>
Fri, 26 May 2023 05:48:40 +0000 (08:48 +0300)
committerGitHub <noreply@github.com>
Fri, 26 May 2023 05:48:40 +0000 (22:48 -0700)
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>
Lib/concurrent/futures/thread.py
Misc/NEWS.d/next/Library/2023-02-18-22-55-48.gh-issue-102024.RUmg_D.rst [new file with mode: 0644]

index 51c942f51abd371e80ffa07c2b212336afb8eae2..3b3a36a5093336cfd9491d3cf269d49fdac9dd63 100644 (file)
@@ -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()
diff --git a/Misc/NEWS.d/next/Library/2023-02-18-22-55-48.gh-issue-102024.RUmg_D.rst b/Misc/NEWS.d/next/Library/2023-02-18-22-55-48.gh-issue-102024.RUmg_D.rst
new file mode 100644 (file)
index 0000000..bb9e28e
--- /dev/null
@@ -0,0 +1 @@
+Reduce calls of ``_idle_semaphore.release()`` in :func:`concurrent.futures.thread._worker`.