Skip to the page

Python concurrency · Trade-off

Threads, processes or asyncio

The 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

  1. Why not asyncio for the images?
  2. Your async code needs a library that only has a sync client. What do you do?
  3. How many worker processes on an 8-core machine, and why?
  4. 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 ThreadPoolExecutor with a sync client (simpler for hundreds at once). Either way, respect the partner's rate limit.
  • Image resizing (CPU-bound): a ProcessPoolExecutor with 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() or run_in_executor for sync libraries; a process pool for CPU work.
  • Costs: processes need pickling and more memory, and since 3.14 Linux starts them with forkserver by default (macOS and Windows use spawn); 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 voice

Practise 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.

Start your free interview