From 56949eaacebf46cd10ffc1d627b8daeda2b4bfa9 Mon Sep 17 00:00:00 2001 From: Andrii Kuzmin Date: Sat, 18 Feb 2023 09:37:53 +0200 Subject: [PATCH 1/3] gh-102024: Reduced _idle_semaphore.release calls _idle_semaphore.release() is called if only work_queue is empty --- Lib/concurrent/futures/thread.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/Lib/concurrent/futures/thread.py b/Lib/concurrent/futures/thread.py index 51c942f51abd37..3f288bb1080c96 100644 --- a/Lib/concurrent/futures/thread.py +++ b/Lib/concurrent/futures/thread.py @@ -43,7 +43,7 @@ def _python_exit(): 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 issue16284 + del work_item continue executor = executor_reference() From 09e1c7954c441e0b8dfe8281c4901346b0352986 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 18 Feb 2023 22:55:49 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-02-18-22-55-48.gh-issue-102024.RUmg_D.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-02-18-22-55-48.gh-issue-102024.RUmg_D.rst 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 index 00000000000000..bb9e28e06c5554 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-02-18-22-55-48.gh-issue-102024.RUmg_D.rst @@ -0,0 +1 @@ +Reduce calls of ``_idle_semaphore.release()`` in :func:`concurrent.futures.thread._worker`. From 0f50fe55774c480de4a6ef9953dc21970c412c40 Mon Sep 17 00:00:00 2001 From: Andrii Kuzmin Date: Mon, 20 Feb 2023 12:22:53 +0200 Subject: [PATCH 3/3] Changed issue reference issue16284 -> GH-60488 in Lib/concurrent/futures/thread.py Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com> --- Lib/concurrent/futures/thread.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/concurrent/futures/thread.py b/Lib/concurrent/futures/thread.py index 3f288bb1080c96..3b3a36a5093336 100644 --- a/Lib/concurrent/futures/thread.py +++ b/Lib/concurrent/futures/thread.py @@ -90,7 +90,7 @@ def _worker(executor_reference, work_queue, initializer, initargs): if work_item is not None: work_item.run() - # Delete references to object. See issue16284 + # Delete references to object. See GH-60488 del work_item continue