Jump to content

Python: Difference between revisions

25 bytes removed ,  11 December 2020
Line 336: Line 336:
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import ThreadPoolExecutor


executor = ThreadPoolExecutor(max_workers=5 * len(os.sched_getaffinity(0)))
executor = ThreadPoolExecutor(max_workers=2 * os.cpu_count())
def do_something(a, b):
def do_something(a, b):
   return a + b
   return a + b
Line 346: Line 346:
</syntaxhighlight>
</syntaxhighlight>


* <code>len(os.sched_getaffinity(0))</code> returns the number of threads available to the Python process
* <code>len(os.sched_getaffinity(0))</code> returns the number of threads available to the Python process.
* Starting in Python 3.5, if <code>max_workers</code> is none, it defaults to 5 times the number of threads on the system.
* Starting in Python 3.5, if <code>max_workers</code> is none, it defaults to <code>5 * os.cpu_count()</code>.


==Data Structures==
==Data Structures==