Loading lesson…
async/await lets one program wait on many things at once. Perfect for HTTP calls and LLM APIs. Let AI help you avoid the common traps.
async is about waiting efficiently, not running faster on many CPUs. One event loop juggles hundreds of network calls by pausing each one while it waits.
import asyncio
import httpx
async def fetch(client: httpx.AsyncClient, url: str) -> dict:
r = await client.get(url, timeout=10)
r.raise_for_status()
return r.json()
async def main(urls: list[str]) -> list[dict]:
async with httpx.AsyncClient() as client:
return await asyncio.gather(*(fetch(client, u) for u in urls))
if __name__ == "__main__":
data = asyncio.run(main([
"https://httpbin.org/json",
"https://httpbin.org/uuid",
]))
print(data)httpx.AsyncClient plus asyncio.gather fetches many URLs concurrently without threads.async def fetch_limited(sem: asyncio.Semaphore, client: httpx.AsyncClient, url: str):
async with sem:
r = await client.get(url)
return r.json()
async def many(urls: list[str]) -> list[dict]:
sem = asyncio.Semaphore(10) # at most 10 in flight
async with httpx.AsyncClient() as client:
return await asyncio.gather(*(fetch_limited(sem, client, u) for u in urls))Semaphores cap how many coroutines run at once. Essential when hitting rate-limited APIs.Understanding "Python Async With AI" in practice: AI-assisted coding shifts work from syntax recall to design thinking — models handle boilerplate so you focus on architecture. Async/await lets one program wait on many things at once. Perfect for HTTP calls and LLM APIs. Let AI help you avoid the common traps — and knowing how to apply this gives you a concrete advantage.
The big idea: async is the right tool when you wait more than you compute. Bound concurrency, stay inside one run() call, and never block the loop.
15 questions · take it digitally for instant feedback at tendril.neural-forge.io/learn/quiz/end-progx-python-async-creators
What is the primary performance benefit of using async/await in Python?
Which code snippet correctly pauses for 3 seconds within an async function?
What is the purpose of a semaphore in asynchronous Python code?
What happens when you nest one asyncio.run() call inside another?
Which statement best describes what an event loop does?
You need to make 100 API calls. What does async allow you to do that synchronous code cannot?
When is async programming the right tool for a task?
What does asyncio.gather() do when passed multiple coroutines?
What is a common pitfall when calling external APIs in a loop using async?
What does 'bounded concurrency' mean in async programming?
Why is calling asyncio.run() only once at program startup recommended?
Which scenario best demonstrates async's strength over synchronous code?
What is wrong with this code: async def fetch(): return await requests.get(url)?
How does an event loop handle 500 concurrent HTTP requests?
What happens if you use await on a coroutine but forget to await a different one in your async function?