Skip to content
dreamcode
dreamcode
Map
Concurrency
Lesson 65 of 77
+15 XP on finish
PYTHON EXPERTChapter 11 · Python Expert

Asynchronous task execution

Use asyncio to write concurrent code using the async and await syntax. Cooperative multitasking yields control back to the event loop during I/O operations.

Worked example

How it reads

  • await asyncio.gather(...) runs multiple coroutines concurrently
  • asyncio.sleep yields control back to the loop without blocking
Cloud tip: Multithreading is CPU-bound limited by the GIL in Python; asyncio is perfect for I/O-bound concurrency.
main.py
PYTHON
real Python, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Add a third call so asyncio.gather runs delay_print("C") too. The output should be A, B, C.

Press Run to check your work.