How to use Python multiprocessing for parallel tasks
Answered
47
My script is CPU-bound and slow. How can I use multiple cores?
Y
Asked by
youssef_net
Platinum
•
311 rep
1 Answer
13
from multiprocessing import Pool, cpu_count
def process_item(item):
# CPU-intensive task
return item * item
# Using Pool
items = list(range(1000))
with Pool(processes=cpu_count()) as pool:
results = pool.map(process_item, items)
# For more control
from concurrent.futures import ProcessPoolExecutor
with ProcessPoolExecutor(max_workers=4) as executor:
results = list(executor.map(process_item, items))
D
Silver
•
415 rep
Your Answer
You need to be logged in to answer questions.
Log In to Answer