PrepPilotPractise free

Async & Concurrency in Python interview questions

asyncio, threads vs processes, GIL, concurrency patterns

17 questions · asyncio · updated November 2026

Practise these as flashcards. One or two lines each; the ones you miss come back first.

Start free

Core questions

What is a coroutine in Python, and how do you define one?

A 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.

How does the asyncio event loop work?

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.

What does the 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.

How do you run multiple coroutines concurrently in asyncio?

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.

Intermediate questions

What happens if you call a blocking function in an async coroutine?

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.

How can you avoid blocking the event loop when you need to run a CPU-bound or blocking operation?

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.

What is the difference between threading and multiprocessing in Python?

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.

How do you implement a timeout for an async operation?

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.

What is task cancellation in asyncio, and how do you handle it?

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.

Why is the GIL important when choosing between threads and processes?

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.

Name an async database or HTTP client library and explain why it's important.

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.

How do you mix async and blocking code in a real-world application?

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.

Senior questions

Describe a common bug in async Python code and how you would debug it.

See the answer in the app, free

How would you design an async producer-consumer pipeline in Python?

See the answer in the app, free

Explain the trade-offs between using asyncio, threading, and multiprocessing for concurrency.

See the answer in the app, free

What changes are coming with free-threaded Python, and how will it impact concurrency?

See the answer in the app, free

How do you ensure your async code is safe from race conditions and resource leaks?

See the answer in the app, free

More Python topics