Skip to the page

Concurrency · mid

Threads, processes, asyncio and free-threading: which one and why

I/O-bound work uses asyncio or threads, CPU-bound pure Python uses processes, and futures hide errors until you read them. Python 3.14 changed how worker processes start on Linux.

The notes

What you must know

  • 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. Respect the partner's rate limit.
  • CPU-bound pure Python (resizing images, building PDFs): a ProcessPoolExecutor with about one worker per core. Threads only help if the library releases the GIL.
  • 10,000 open connections: asyncio, because an operating system thread for each costs memory and switching.
  • Mix: asyncio.to_thread or run_in_executor for sync libraries; a process pool for CPU work.
  • Processes cost pickling and memory: send ids, not big objects, use chunksize, an initializer to load fonts or config once per worker, and max_tasks_per_child. Make re-runs safe with deterministic output names and recorded progress.
  • Since 3.14 Linux starts worker processes with forkserver by default (macOS and Windows use spawn). Workers no longer inherit what the parent set at runtime. Pass config through initializer or load it in each worker.
  • executor.submit returns a Future. An exception inside the task is stored and raised only when someone calls .result(). Keep the futures, loop over as_completed, log failures with logger.exception, and exit non-zero if any failed.
  • Free-threaded build and subinterpreters (PEP 734) are options for CPU-heavy code. Every C extension needs free-threaded support. Today processes are the proven choice. Decide by measurement.

The red pen

Common traps: mistake → fix

  • Mistake: asyncio makes everything faster

    Fix: it helps with waiting, not with CPU work.

  • Mistake: Use more threads for a CPU-bound job

    Fix: 16 threads on pure Python took 2 hours 10 minutes instead of 2 hours.

  • Mistake: The with block waits for the pool, so errors would show

    Fix: errors stay inside the futures until read.

  • Mistake: The GIL is gone in 3.14

    Fix: free-threading is optional and many libraries are not ready.

Easy to hard

Questions interviewers ask, easy to hard

  1. Threads, processes or asyncio for 20,000 PAN checks, 50,000 image resizes and 10,000 WebSocket connections?
  2. Why did 16 threads make a PDF job slower?
  3. This upload job always logs "all files processed" but some files never arrive. Why?
  4. What changed for multiprocessing on Linux in Python 3.14?
  5. Free-threading, subinterpreters or processes for a CPU-heavy pricing engine today?

Practise

Practise with these questions

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview