2. Asynchronous Programming:
Using async and await for concurrent tasks with asyncio.
Using async and await for concurrent tasks with asyncio.
1. Basic Async Function
import asyncio
async def say_hello():
print("Hello, World!")
await asyncio.sleep(1)
print("Goodbye, World!")
asyncio.run(say_hello())2. Running Multiple Tasks Concurrently
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.sleep for Delays
4. Concurrent HTTP Requests with aiohttp
5. Producer-Consumer Pattern
6. Timeouts with asyncio.wait_for
7. Asynchronous File I/O with aiofiles
8. Using asyncio.Semaphore to Limit Concurrency
9. Event Loop with asyncio.create_task
10. Using asyncio.Queue for Communication Between Tasks
Last updated