Skip to content

Instantly share code, notes, and snippets.

@pamelafox
Created May 1, 2025 17:15
Show Gist options
  • Save pamelafox/52a9e05f2fbf45b14fdd520bcb02e078 to your computer and use it in GitHub Desktop.
Save pamelafox/52a9e05f2fbf45b14fdd520bcb02e078 to your computer and use it in GitHub Desktop.
AsyncIO example - concurrency in task group
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