tezvyn:

Explain the asyncio event loop and cooperative multitasking

AI-drafted, machine-checkedSource: docs.python.orgintermediate
Explain the asyncio event loop and cooperative multitasking

Tests if you view the event loop as a single-threaded orchestrator, not magic parallelism. Strong answers note it runs tasks and callbacks, manages a ready queue, and yields control at await. Red flag: calling it multithreading or parallel execution.

WHAT THIS TESTS: The interviewer wants to know if you understand that asyncio concurrency is cooperative and single-threaded. They are checking whether you can separate the event loop's orchestration role from OS preemption or parallelism, and whether you know where control handoffs actually happen. Specifically, they care if you realize that the loop is not executing code simultaneously but rather juggling suspended execution contexts.

A GOOD ANSWER COVERS: First, define the event loop as the core runtime that schedules and runs asynchronous tasks and callbacks. Second, list its primary responsibilities: maintaining a queue of ready tasks, executing callbacks via call_soon and call_later, and polling the OS selector for I/O readiness. Third, explain cooperative multitasking: coroutines run until they hit an await expression, at which point they yield control back to the loop by returning a Future or Task to the scheduler. The loop then picks the next ready task from the queue. Fourth, emphasize that this is not parallelism; only one coroutine runs at a time per thread, and context switches happen only at await boundaries, not by force. Fifth, mention that application code usually interacts with the loop indirectly through asyncio.run() or high-level APIs, but the loop object itself is what drives execution.

COMMON WRONG ANSWERS: Calling the event loop multithreading or saying it runs coroutines in parallel. Claiming that await creates a new thread. Describing it as preemptive time-slicing like an OS scheduler. Saying the loop is just a while True without mentioning the selector, callback queue, or Task scheduling. Confusing the high-level asyncio.run() abstraction with the low-level loop mechanics. Asserting that coroutines run in the background automatically without being scheduled as Tasks.

LIKELY FOLLOW-UPS: How does the loop handle blocking calls? What is the difference between a coroutine and a Task? How would you run an event loop in a secondary thread? What happens if a coroutine never awaits? How does the ProactorEventLoop differ from SelectorEventLoop on Windows? Why might you call loop.create_future() instead of asyncio.Future() directly?

ONE CONCRETE EXAMPLE: Imagine a FastAPI endpoint that awaits two database queries. When the first query awaits the network I/O, the coroutine suspends and the event loop schedules the second query's coroutine. While both queries are in flight, the loop polls the OS selector. As soon as the first query's socket reports readiness, the loop resumes that coroutine. No thread per request is needed because the loop interleaves them cooperatively, keeping CPU and memory overhead low.

Source: docs.python.org

Read the original → docs.python.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.