From Async/Await to Virtual Threads
9 months ago
- #Python
- #VirtualThreads
- #Concurrency
- The author discusses the benefits of threads over async/await in Python, highlighting how async/await has exposed more people to concurrent programming but introduced complexity.
- Threads are simpler but traditional threading APIs have limitations. Async/await improved on these but added complexity with both async and threading systems.
- Structured concurrency, introduced with async/await, prevents tasks from outliving their parents, improving flow and context management compared to traditional threads.
- Robust cancellations are challenging, especially with real threads, as seen with libraries like aiofiles, leading to potential deadlocks and poor developer experiences.
- The author proposes rethinking concurrency by embracing threads with better APIs, possibly using virtual threads to address performance issues.
- Virtual threads would require handling blocking operations by yielding to the scheduler, maintaining structured concurrency without losing its benefits.
- Example code demonstrates how virtual threads with structured concurrency could simplify concurrent programming, avoiding futures and promises.
- The complexity of concurrent programming should be managed by the interpreter and internal APIs, hiding it from most programmers.
- The author suggests improving threading APIs, like using mutexes and semaphores more effectively, and limiting concurrency within thread groups.
- The future of async/await is debatable, but the author advocates for exploring virtual threads to eliminate the need for colored functions.