Lesson 42 of 55 · Python
AsyncIO Basics
Duration: 20 minutes
AsyncIO Basics\n\nasyncio provides the building blocks for asynchronous, non‑blocking code using the async/await syntax. It shines for I/O‑bound workloads like network calls.\n\n```python\nimport asyncio\n\nasync def greet(name):\n await asyncio.sleep(1) # simulate async I/O\n return f'Hello, {name}!'
\nasync def main():\n messages = await asyncio.gather(greet('Alice'), greet('Bob'))\n for m in messages:\n print(m)\n\nasyncio.run(main())\n```\n\nThe event loop (asyncio.run) schedules coroutines and runs them concurrently.\n