91. Event Loop in asyncio
1. Basic Event Loop Execution
import asyncio
async def greet():
print("Hello, World!")
await asyncio.sleep(1)
print("Goodbye, World!")
asyncio.run(greet())2. Running Multiple Coroutines
import asyncio
async def task1():
print("Task 1 starting...")
await asyncio.sleep(2)
print("Task 1 done.")
async def task2():
print("Task 2 starting...")
await asyncio.sleep(1)
print("Task 2 done.")
async def main():
await asyncio.gather(task1(), task2())
asyncio.run(main())3. Using asyncio.create_task
asyncio.create_task4. Running Tasks Sequentially
5. Using asyncio.sleep for Delays
asyncio.sleep for Delays6. Handling Exceptions in Coroutines
7. Running Tasks with Timeouts
8. Using asyncio.run_until_complete
asyncio.run_until_complete9. Using asyncio.as_completed
asyncio.as_completed10. Running Background Tasks
Last updated