Skip to content

Q&A · Answered

How to use Python multiprocessing for parallel tasks

asked 9 months, 1 week ago 1435 views 1 answer
47

My script is CPU-bound and slow. How can I use multiple cores?

1 Answer

One accepted
13

Accepted answer

Not confirmed since it was written, 9 months, 1 week ago
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))

Your answer

Say what you tried, not only what worked — the next person needs the reasoning. Code goes in a fenced block.

Shown beside your answer and never checked, so it cannot be a name someone already has here.

No account needed to answer. Sign in instead and it carries your name and earns reputation.