asyncio, threads vs processes, GIL, concurrency patterns
Practise these as flashcards. One or two lines each; the ones you miss come back first.
Start freeA coroutine is a special function defined with async def that can be paused and resumed by the event loop. You define it using async def, and it must use await to yield control back to the event loop.
The asyncio event loop manages and schedules coroutines, running them in a single thread. It switches between tasks when they're waiting for I/O, allowing efficient concurrency for I/O-bound operations.
await keyword do?await pauses the coroutine until the awaited async operation completes, letting the event loop run other tasks in the meantime. It's essential for non-blocking async code.
You can use asyncio.gather to run multiple coroutines concurrently and collect their results. Each coroutine is scheduled as a task by the event loop.
Calling a blocking function in an async coroutine stalls the event loop, preventing other tasks from running. This defeats the purpose of async and can severely impact performance.
You can use loop.run_in_executor or concurrent.futures.ThreadPoolExecutor to offload blocking or CPU-bound work to a separate thread or process, keeping the event loop responsive.
Threading uses multiple threads within a single process, but is limited by the GIL for CPU-bound tasks. Multiprocessing uses separate processes, allowing true parallelism and bypassing the GIL.
You can use asyncio.wait_for to wrap a coroutine and specify a timeout. If the operation doesn't complete in time, it raises asyncio.TimeoutError.
Task cancellation means stopping a running coroutine, usually by calling cancel() on the task. You should handle asyncio.CancelledError in your coroutine to clean up resources gracefully.
The GIL prevents multiple threads from executing Python bytecode at once, so for CPU-bound tasks, processes are better. For I/O-bound tasks, threads are fine since the GIL is released during I/O.
Libraries like asyncpg for databases or httpx for HTTP provide async APIs, allowing you to handle many connections efficiently without blocking the event loop. They're crucial for building scalable async services.
I use async libraries for all I/O, and for blocking or CPU-bound code, I offload it to executors or processes. This keeps the event loop responsive and allows mixing async and parallelism efficiently.