Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Doc/library/concurrent.futures.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ And::
.. versionchanged:: 3.7
Added the *initializer* and *initargs* arguments.

.. versionchanged:: 3.8
Default value of *max_workers* is changed to ``min(32, os.cpu_count() + 4)``.
This default value preserves at least 5 workers for I/O bound tasks.
It utilizes at most 32 CPU cores for CPU bound tasks which release the GIL.
And it avoids using very large resources implicitly on many-core machines.

ThreadPoolExecutor now reuses idle worker threads before starting
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the corresponding change in the code? For reusing the idle worker threads before starting max_workers worker threads?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*max_workers* worker threads too.


.. _threadpoolexecutor-example:

Expand Down
11 changes: 8 additions & 3 deletions Lib/concurrent/futures/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,14 @@ def __init__(self, max_workers=None, thread_name_prefix='',
initargs: A tuple of arguments to pass to the initializer.
"""
if max_workers is None:
# Use this number because ThreadPoolExecutor is often
# used to overlap I/O instead of CPU work.
max_workers = (os.cpu_count() or 1) * 5
# ThreadPoolExecutor is often used to:
# * CPU bound task which releases GIL
# * I/O bound task (which releases GIL, of course)
#
# We use cpu_count + 4 for both types of tasks.
# But we limit it to 32 to avoid consuming surprisingly large resource
# on many core machine.
max_workers = min(32, (os.cpu_count() or 1) + 4)
if max_workers <= 0:
raise ValueError("max_workers must be greater than 0")

Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_concurrent_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,8 +755,8 @@ def record_finished(n):

def test_default_workers(self):
executor = self.executor_type()
self.assertEqual(executor._max_workers,
(os.cpu_count() or 1) * 5)
expected = min(32, (os.cpu_count() or 1) + 4)
self.assertEqual(executor._max_workers, expected)

def test_saturation(self):
executor = self.executor_type(4)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Change default *max_workers* of ``ThreadPoolExecutor`` from ``cpu_count() *
5`` to ``min(32, cpu_count() + 4))``. Previous value was unreasonably
large on many cores machines.