Back to Course
Python Essentials: The Engineering Approach
Module 17 of 20
17. AsyncIO & Concurrency
Sync vs Async
- Sync (Blocking): You call a function, the world stops until it returns.
- Async (Non-Blocking): You call a function, it gives you a "Promise" (Future), and you can do other work while waiting.
The Event Loop
Python has a loop that watches for "awaitable" tasks.
pythonimport asyncio async def fetch_data(): print("Start fetching") await asyncio.sleep(1) # Simulates I/O print("Done fetching") return {"data": 1} async def main(): # Run concurrently task1 = asyncio.create_task(fetch_data()) task2 = asyncio.create_task(fetch_data()) await task1 await task2 asyncio.run(main())