async def
: Declares a coroutine function, which can use await
to suspend its execution until the result of an asynchronous operation is available.await
: Suspends the execution of the coroutine until the awaited coroutine or Future completes and returns the result.import asyncio
async def async_task():
print("Task started")
await asyncio.sleep(1)
print("Task completed")
# Running an asynchronous coroutine
asyncio.run(async_task())
Asynchronous I/O operations involve tasks that wait for external resources (e.g., network requests, file operations). Asyncio provides mechanisms to handle these operations efficiently without blocking the event loop.
import aiohttp
import asyncio
async def fetch_data(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
return await response.text()
async def main():
url = 'https://jsonplaceholder.typicode.com/posts/1'
response = await fetch_data(url)
print(response)
asyncio.run(main())
aiohttp.ClientSession
: Manages HTTP connections and provides a context manager for making HTTP requests asynchronously.session.get(url)
: Initiates an asynchronous GET request to the specified URL.response.text()
: Asynchronously retrieves and returns the response body as text.async
and await
.