dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Tip: Multithreading in Python

See how to spin threads quickly and execute tasks in parallel using the ThreadPoolExecutor.


advertisement

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Using ThreadPoolExecutor from the concurrent.futures library, we can spin threads quickly and execute tasks in parallel. Wrap your time consuming method with the thread pool executor..

Without threading:

for task in tasks
# some time consuming operation

With ThreadPoolExecutor:

from concurrent.futures import ThreadPoolExecutor, as_completed
processes = []
with ThreadPoolExecutor(max_workers=10) as executor:
for task in tasks
processes.append(executor.submit(task, argument))
for task in as_completed(processes):
   # see task.result()

es): # see task.result()

 

Visit the DevX Tip Bank

 





   
Srinath M. S. says: "I would love to leave a footprint in this flat world."
Thanks for your registration, follow us on our social networks to keep up-to-date