Python concurrency · Trade-off
Threads, processes or asyncio
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voiceThe interviewer asks
For each of these, would you use threads, processes or asyncio, and why? One: call a partner API for 20,000 PAN numbers. Two: resize 50,000 product images. Three: a service that holds 10,000 open WebSocket connections.
Then
Follow-ups the interviewer asks next
- Why not asyncio for the images?
- Your async code needs a library that only has a sync client. What do you do?
- How many worker processes on an 8-core machine, and why?
- What would the free-threaded build change?
The answer
What a strong answer covers
- Many API calls (I/O-bound): asyncio with an async client and a limit on calls in flight, or a
ThreadPoolExecutorwith a sync client (simpler for hundreds at once). Either way, respect the partner's rate limit. - Image resizing (CPU-bound): a
ProcessPoolExecutorwith about one worker per core; threads only if the library releases the GIL for the heavy part. - 10,000 connections: asyncio, since an OS thread each costs memory and switching.
- Mixing: asyncio plus
asyncio.to_thread()orrun_in_executorfor sync libraries; a process pool for CPU work. - Costs: processes need pickling and more memory, and since 3.14 Linux starts them with
forkserverby default (macOS and Windows usespawn); asyncio needs async libraries all the way down; threads need locks. - Free-threading may make threads work for CPU-bound code once every library supports it.
The red pen
Common traps
Avoid:"asyncio makes everything faster"; one thread or process per item.
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voicePractise questions like this out loud
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.