Jump to content

Python: Difference between revisions

77 bytes added ,  18 March 2022
No edit summary
Line 368: Line 368:
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import ThreadPoolExecutor


executor = ThreadPoolExecutor(max_workers=2 * os.cpu_count())
executor = ThreadPoolExecutor(max_workers=os.cpu_count())
thread_lock = threading.Lock()
thread_lock = threading.Lock()
total = 0
total = 0
Line 390: Line 390:
* <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 <code>5 * os.cpu_count()</code>.
* Starting in Python 3.5, if <code>max_workers</code> is none, it defaults to <code>5 * os.cpu_count()</code>.
** <code>os.cpu_count()</code> returns the number of logical CPUs (i.e. threads)
* <code>executor.shutdown()</code> will wait for all jobs to finish but you cannot submit any additional jobs from other threads, after calling shutdown.
* <code>executor.shutdown()</code> will wait for all jobs to finish but you cannot submit any additional jobs from other threads, after calling shutdown.
* List operations are thread-safe but most other operations will require using a thread lock or semaphore.
* List operations are thread-safe but most other operations will require using a thread lock or semaphore.