Created
May 1, 2025 17:15
-
-
Save pamelafox/52a9e05f2fbf45b14fdd520bcb02e078 to your computer and use it in GitHub Desktop.
AsyncIO example - concurrency in task group
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import httpx | |
async def fetch_url(url): | |
print(f"Fetching {url}") | |
async with httpx.AsyncClient(timeout=httpx.Timeout(10.0)) as client: | |
r = await client.get(url) | |
print(f"Received {url} with status code {r.status_code}") | |
return r.text | |
async def main(): | |
async with asyncio.TaskGroup() as tg: | |
tg.create_task(fetch_url("https://httpbin.org/delay/3")) | |
tg.create_task(fetch_url("https://httpbin.org/delay/1")) | |
asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment