BENEDICT NEO梁耀恩

async vs thread

September 6, 2025·#programming#learning#tech·1 min read·

one thing i had a ton of issues with in my code quality was not using modern python typing preferences - list, dict over List, Dict in 3.9+. the root of that is because claude code is just trained on a bunch of old code, and using claude code extensively, i got flamed in my review. which was very helpful because now i know better. i love making mistakes, even though it comes at the expense of my pride—that's something i should let go of more often.

also learned about the benefits of asyncio over threadpool in python. essentially it uses a lot less memory because there's only one thread managing the event loop, versus threadpool which spawns multiple threads.

                    ASYNCIO (Single Thread)

                      [Event Loop]

         ┌─────────────────┼─────────────────┐
         ▼                 ▼                 ▼
    ┌─────────┐       ┌─────────┐       ┌─────────┐
    │  Task 1 │       │  Task 2 │       │  Task 3 │
    │  [API1] │       │  [API2] │       │  [API3] │  ← all tasks
    │ waiting │       │ waiting │       │ waiting │    started
    └─────────┘       └─────────┘       └─────────┘    instantly

    Memory: ~1 thread, minimal overhead
              THREADPOOL (Multiple Threads)

    ┌──────────┬──────────┬──────────┬──────────┐
    │ Thread 1 │ Thread 2 │ Thread 3 │ Thread 4 │
    ├──────────┼──────────┼──────────┼──────────┤
    │  [API1]  │  [API2]  │  [API3]  │  [API4]  │ ← 4 tasks
    │  waiting │  waiting │  waiting │  waiting │   running
    │          │          │          │          │
    │  [API5]  │  [API6]  │   idle   │   idle   │ ← tasks 5-6
    │  waiting │  waiting │          │          │   must wait
    └──────────┴──────────┴──────────┴──────────┘

    Memory: ~4 threads, higher overhead per thread