Async Python

Async/Await Basics

Async/Await Basics

Asynchronous programming is essential for AI applications that make API calls and handle streaming responses.

Why Async?

  • Non-blocking — Don't wait for slow operations
  • Concurrent — Handle multiple requests simultaneously
  • Streaming — Process data as it arrives

Key Concepts

async def my_function():
    result = await some_async_operation()
    return result
  • `async def` — Declares an async function
  • `await` — Pauses until the operation completes
  • `asyncio` — Python's async library

Common Patterns

  • Making async API calls
  • Processing streaming responses
  • Running concurrent requests
  • Handling timeouts

Try It Yourself

Run this code example to practice what you've learned.

example.py
1import asyncio
2from typing import AsyncIterator
3
4# Basic async function
5async def fetch_response(prompt: str) -> str:
6 """Simulate an async API call."""
7 print(f"Sending: {prompt}")
8 await asyncio.sleep(1) # Simulate network delay
9 return f"Response to: {prompt}"
10
11# Async generator for streaming
12async def stream_response(prompt: str) -> AsyncIterator[str]:
13 """Simulate streaming response."""
14 words = ["Hello", "!", " I", " am", " an", " AI", " assistant", "."]
15 for word in words:
16 await asyncio.sleep(0.1) # Simulate token delay
17 yield word
18
19# Running async code
20async def main():
21 # Single async call
22 response = await fetch_response("What is Python?")
23 print(f"Got: {response}\n")
24
25 # Streaming response
26 print("Streaming: ", end="")
27 async for token in stream_response("Tell me a story"):
28 print(token, end="", flush=True)
29 print("\n")
30
31 # Concurrent calls
32 prompts = ["Question 1", "Question 2", "Question 3"]
33 tasks = [fetch_response(p) for p in prompts]
34 results = await asyncio.gather(*tasks)
35 print("Concurrent results:", results)
36
37# Run the async main function
38asyncio.run(main())