mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Closes #21527: Add default number of workers to ThreadPoolExecutor. (Claudiu Popa.)
This commit is contained in:
parent
8257b6283e
commit
cfd4661e78
4 changed files with 24 additions and 2 deletions
|
@ -10,6 +10,7 @@ from concurrent.futures import _base
|
|||
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,17 @@ def _worker(executor_reference, work_queue):
|
|||
_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:
|
||||
# Use this number because ThreadPoolExecutor is often
|
||||
# used to overlap I/O instead of CPU work.
|
||||
max_workers = (os.cpu_count() or 1) * 5
|
||||
if max_workers <= 0:
|
||||
raise ValueError("max_workers must be greater than 0")
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue