Skip to main content
Editor

Python Threading vs Multiprocessing vs asyncio: When to Use What

Advanced30 min5 exercises100 XP
0/5 exercises

Most programs you've written so far do one thing at a time. But real applications need to handle multiple tasks: downloading files while updating a progress bar, serving web requests from many users, or crunching numbers across all CPU cores.

Python gives you three tools for this: threading (concurrent tasks sharing memory), multiprocessing (parallel tasks in separate processes), and asyncio (cooperative multitasking for I/O). Each solves different problems, and choosing wrong means either bugs or wasted performance.

What Is the Difference Between Concurrency and Parallelism?

These two terms are often confused, but they mean different things. Concurrency is about dealing with multiple tasks at once - they might take turns on a single CPU core. Parallelism is about doing multiple tasks at once - they actually run simultaneously on different cores.

Analogy: A single chef juggling three pots on the stove is concurrency. Three chefs each working on their own dish is parallelism.

Sequential vs concurrent, timed
import asyncio
import time
async def do_task(name, duration):
await asyncio.sleep(duration) # waiting, e.g. for a download
return f'{name} done ({duration}s)'
tasks = [('Download', 0.6), ('Parse', 0.3), ('Save', 0.3)]
# Sequential: one task after another
start = time.time()
for name, duration in tasks:
print(await do_task(name, duration))
print(f'Sequential: {time.time() - start:.1f}s')
# Concurrent: the waits overlap
start = time.time()
results = await asyncio.gather(*(do_task(n, d) for n, d in tasks))
print(results)
print(f'Concurrent: {time.time() - start:.1f}s')
Output
Download done (0.6s)
Parse done (0.3s)
Save done (0.3s)
Sequential: 1.2s
['Download done (0.6s)', 'Parse done (0.3s)', 'Save done (0.3s)']
Concurrent: 0.6s

How Does Threading Work in Python?

A thread is a lightweight unit of execution within a process. Multiple threads share the same memory space, which makes communication easy but introduces the risk of data races.

Thread basics (simulated)
import threading
# Basic thread creation
def worker(name, count):
results = []
for i in range(count):
results.append(f'{name}: step {i + 1}')
return results
# Simulating thread-like behavior
# (threads cannot start in the browser)
class SimpleThread:
def __init__(self, target, args=()):
self.target = target
self.args = args
self.result = None
def start(self):
self.result = self.target(*self.args)
def join(self):
pass # Already completed in our simulation
# Create and run threads
t1 = SimpleThread(target=worker, args=('Thread-1', 3))
t2 = SimpleThread(target=worker, args=('Thread-2', 3))
t1.start()
t2.start()
t1.join()
t2.join()
for step in t1.result:
print(step)
for step in t2.result:
print(step)
Output
Thread-1: step 1
Thread-1: step 2
Thread-1: step 3
Thread-2: step 1
Thread-2: step 2
Thread-2: step 3

In real Python, threading.Thread creates an actual OS thread that runs concurrently. The start() method begins execution, and join() waits for the thread to finish. In our browser environment, we simulate this pattern.

Why Do You Need Locks?

When multiple threads access shared data, things can go wrong. Imagine two threads both reading a counter at 5, both adding 1, and both writing 6. The counter should be 7 but it's 6 - this is a race condition.

Locks prevent race conditions
import threading
# Demonstrating the race condition concept
class BankAccount:
def __init__(self, balance):
self.balance = balance
self.lock = threading.Lock()
def withdraw_unsafe(self, amount):
"""Unsafe: no lock. In real threading, this causes races."""
current = self.balance
# In real threads, another thread could modify balance HERE
self.balance = current - amount
def withdraw_safe(self, amount):
"""Safe: uses a lock to prevent concurrent access."""
with self.lock:
if self.balance >= amount:
current = self.balance
self.balance = current - amount
return True
return False
account = BankAccount(100)
# Simulate 5 withdrawals of $30 from $100
successes = 0
for _ in range(5):
if account.withdraw_safe(30):
successes += 1
print(f'Successful withdrawals: {successes}')
print(f'Remaining balance: ${account.balance}')
print(f'The lock prevented overdrawing!')
Output
Successful withdrawals: 3
Remaining balance: $10
The lock prevented overdrawing!

A Lock (mutex) ensures only one thread can access a block of code at a time. The with self.lock: statement acquires the lock, executes the code, and releases it automatically - even if an exception occurs.

Threading vs Multiprocessing vs asyncio: Decision Guide

Choosing the right concurrency tool depends on your workload type. Here's the decision tree professional Python developers use:

Concurrency decision guide
# Decision guide as a function
def recommend_approach(task_type):
"""Recommend the best concurrency approach for a task type."""
recommendations = {
'io_bound': {
'tool': 'threading or asyncio',
'examples': 'HTTP requests, file I/O, database queries',
'why': 'Threads release the GIL during I/O waits'
},
'cpu_bound': {
'tool': 'multiprocessing',
'examples': 'Image processing, math, data analysis',
'why': 'Separate processes bypass the GIL'
},
'many_io_tasks': {
'tool': 'asyncio',
'examples': 'Web servers, chat apps, 1000+ connections',
'why': 'Lower overhead than threads for many tasks'
},
'simple_background': {
'tool': 'threading',
'examples': 'Progress bars, periodic saves, timers',
'why': 'Easiest to add to existing code'
},
}
rec = recommendations.get(task_type, {'tool': 'sequential', 'examples': 'N/A', 'why': 'Unknown task type'})
return rec
for task in ['io_bound', 'cpu_bound', 'many_io_tasks', 'simple_background']:
rec = recommend_approach(task)
print(f'{task}:')
print(f' Use: {rec["tool"]}')
print(f' Examples: {rec["examples"]}')
print()
Output
io_bound:
  Use: threading or asyncio
  Examples: HTTP requests, file I/O, database queries

cpu_bound:
  Use: multiprocessing
  Examples: Image processing, math, data analysis

many_io_tasks:
  Use: asyncio
  Examples: Web servers, chat apps, 1000+ connections

simple_background:
  Use: threading
  Examples: Progress bars, periodic saves, timers

What Is asyncio and How Does It Work?

asyncio uses cooperative multitasking: instead of the OS switching between threads, your code explicitly says "I'm waiting, let someone else run" using the await keyword. This is more efficient than threading for I/O-heavy workloads because there's no thread switching overhead.

Three downloads that overlap
import asyncio
async def fetch(name, delay):
print(f'{name}: connecting')
await asyncio.sleep(delay) # waiting for the network
print(f'{name}: downloading')
await asyncio.sleep(delay)
print(f'{name}: done')
return name
results = await asyncio.gather(
fetch('fetch_users', 0.35),
fetch('fetch_posts', 0.2),
fetch('fetch_comments', 0.15),
)
print('Finished:', results)
Output
fetch_users: connecting
fetch_posts: connecting
fetch_comments: connecting
fetch_comments: downloading
fetch_posts: downloading
fetch_comments: done
fetch_users: downloading
fetch_posts: done
fetch_users: done
Finished: ['fetch_users', 'fetch_posts', 'fetch_comments']

Each await asyncio.sleep() stands in for waiting on the network. While one coroutine waits, the event loop runs the others, so their steps interleave and the whole run takes about as long as the slowest one (0.7s), not the sum. gather() still returns the results in the order you passed the coroutines. (On this page you can await at the top level; in a script, put this inside async def main() and call asyncio.run(main()).)

Synchronous code
import requests

def get_pages(urls):
    results = []
    for url in urls:
        r = requests.get(url)  # Blocks!
        results.append(r.text)
    return results
# Total time: sum of all requests
Async code (conceptual)
import aiohttp, asyncio

async def get_pages(urls):
    async with aiohttp.ClientSession() as s:
        tasks = [s.get(u) for u in urls]
        results = await asyncio.gather(*tasks)
    return results
# Total time: longest single request

What Is ThreadPoolExecutor?

Creating and managing threads manually is error-prone. ThreadPoolExecutor from concurrent.futures provides a high-level API: submit tasks, get results, and the pool manages the threads for you.

ThreadPoolExecutor for clean concurrent code
from concurrent.futures import ThreadPoolExecutor

def process_item(item):
    """Simulate processing an item."""
    return f'Processed: {item.upper()}'

items = ['alpha', 'beta', 'gamma', 'delta', 'epsilon']

# Using ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=3) as executor:
    # map() applies the function to each item
    results = list(executor.map(process_item, items))

for r in results:
    print(r)

Threads cannot start in the browser, so run this one on your own computer: it prints Processed: ALPHA through Processed: EPSILON, in order. The with statement ensures threads are properly cleaned up. executor.map() works just like the built-in map() but runs tasks across a pool of threads. Results come back in the same order as the inputs.


Practice Exercises

Concurrency Decision Maker
Write Code

Write a function classify_task(description) that takes a task description string and returns the recommended concurrency tool.

Rules:

  • If the description contains "download", "request", "api", or "file" (case-insensitive), return "threading"
  • If the description contains "calculate", "process", "compute", or "analyze" (case-insensitive), return "multiprocessing"
  • If the description contains "server", "websocket", or "connections" (case-insensitive), return "asyncio"
  • Otherwise, return "sequential"
  • Test with these descriptions:

  • "Download 100 files from S3"
  • "Calculate prime numbers"
  • "Handle 5000 WebSocket connections"
  • "Print hello world"
  • # Write the classify_task function
    # Test with the four descriptions
    Build a Thread-Safe Counter
    Write Code

    Create a SafeCounter class with:

  • An __init__ that sets self.value = 0 and creates a threading.Lock()
  • An increment() method that uses the lock to safely add 1 to value
  • A get() method that returns the current value
  • Create a counter, call increment() 100 times in a loop, and print the final value.

    import threading
    # Create SafeCounter class with lock-based increment
    # Create counter, increment 100 times, print result
    Predict the Output: asyncio.gather
    Predict Output

    What does this code print? Note that asyncio.gather runs the coroutines concurrently but returns their results in the order you passed them in.

    import asyncio
    
    async def double(x):
        return x * 2
    
    numbers = [5, 10, 15, 20]
    results = await asyncio.gather(*(double(n) for n in numbers))
    print(results)
    print(sum(results))
    import asyncio
    async def double(x):
    return x * 2
    numbers = [5, 10, 15, 20]
    results = await asyncio.gather(*(double(n) for n in numbers))
    print(results)
    print(sum(results))
    Fix the Bug: Missing Lock
    Fix the Bug

    This SharedList class is meant to be thread-safe, but the add() method doesn't use the lock. Fix the code so that add() and get_all() both use the lock properly.

    After the fix, add three items and print the list.

    import threading
    class SharedList:
    def __init__(self):
    self.items = []
    self.lock = threading.Lock()
    def add(self, item):
    # Bug: no lock protection!
    self.items.append(item)
    def get_all(self):
    with self.lock:
    return list(self.items)
    shared = SharedList()
    shared.add('alpha')
    shared.add('beta')
    shared.add('gamma')
    print(shared.get_all())
    Build a Task Queue
    Write Code

    Build a TaskQueue class that simulates a concurrent task processor:

    1. __init__ creates an empty list self.tasks and an empty list self.results

    2. add_task(name, func, arg) appends a tuple (name, func, arg) to tasks

    3. run_all() processes each task by calling func(arg), storing (name, result) in results

    4. get_results() returns the results list

    Create a queue, add three tasks:

  • ("square", lambda x: x**2, 5)
  • ("double", lambda x: x*2, 7)
  • ("negate", lambda x: -x, 3)
  • Run all tasks and print each result as "{name}: {result}".

    # Build the TaskQueue class
    # Create queue, add tasks, run, print results

    Related Tutorials

    Was this tutorial helpful?