diff -r ec3ca1901776 Doc/library/concurrent.futures.rst --- a/Doc/library/concurrent.futures.rst Sun May 18 21:40:20 2014 +0100 +++ b/Doc/library/concurrent.futures.rst Mon May 19 01:38:40 2014 +0300 @@ -115,11 +115,15 @@ executor.submit(wait_on_future) -.. class:: ThreadPoolExecutor(max_workers) +.. class:: ThreadPoolExecutor(max_workers=None) An :class:`Executor` subclass that uses a pool of at most *max_workers* threads to execute calls asynchronously. + .. versionchanged:: 3.5 + If *max_workers* is ``None`` or + not given, it will default to the number of processors on the machine. + .. _threadpoolexecutor-example: diff -r ec3ca1901776 Lib/concurrent/futures/thread.py --- a/Lib/concurrent/futures/thread.py Sun May 18 21:40:20 2014 +0100 +++ b/Lib/concurrent/futures/thread.py Mon May 19 01:38:40 2014 +0300 @@ -10,6 +10,7 @@ import queue import threading import weakref +import os # Workers are created as daemon threads. This is done to allow the interpreter # to exit when there are still idle threads in a ThreadPoolExecutor's thread @@ -80,13 +81,15 @@ _base.LOGGER.critical('Exception in worker', exc_info=True) class ThreadPoolExecutor(_base.Executor): - def __init__(self, max_workers): + def __init__(self, max_workers=None): """Initializes a new ThreadPoolExecutor instance. Args: max_workers: The maximum number of threads that can be used to execute the given calls. """ + if max_workers is None: + max_workers = os.cpu_count() or 1 if max_workers <= 0: raise ValueError("max_workers must be greater than 0") diff -r ec3ca1901776 Lib/test/test_concurrent_futures.py --- a/Lib/test/test_concurrent_futures.py Sun May 18 21:40:20 2014 +0100 +++ b/Lib/test/test_concurrent_futures.py Mon May 19 01:38:40 2014 +0300 @@ -11,6 +11,7 @@ from test.script_helper import assert_python_ok +import os import sys import threading import time @@ -444,6 +445,10 @@ self.executor.shutdown(wait=True) self.assertCountEqual(finished, range(10)) + def test_default_workers(self): + executor = self.executor_type() + self.assertEqual(executor._max_workers, os.cpu_count() or 1) + class ProcessPoolExecutorTest(ProcessPoolMixin, ExecutorTest, unittest.TestCase): def test_killed_child(self):