This demonstration shows how to implement request cancellation in Dynamo using:
- Client:
context.stop_generating()to cancel requests - Middle Server: Forwards requests and passes context through (optional)
- Backend Server:
context.is_stopped()to check for cancellation
The demo supports two architectures:
Direct Connection (Default):
Client -> Backend Server
With Middle Server:
Client -> Middle Server -> Backend Server
The middle server acts as a proxy that:
- Receives requests from clients
- Forwards them to backend servers
- Passes the original context through for cancellation support
- Streams responses back to the client
- Start the backend server:
python3 server.py- Run the client (connects directly to backend):
python3 client.py- Start the backend server:
python3 server.py- Start the middle server:
python3 middle_server.py- Run the client (connects through middle server):
python3 client.py --middle- Backend server generates numbers 0-999 with 0.1 second delays
- Client receives the first 3 numbers (0, 1, 2) directly from backend
- Client calls
context.stop_generating()to cancel - Backend server detects cancellation via
context.is_stopped()and stops - Both client and server handle the cancellation gracefully
- Backend server generates numbers 0-999 with 0.1 second delays
- Middle server forwards requests and passes context through
- Client receives the first 3 numbers (0, 1, 2) via the middle server
- Client calls
context.stop_generating()to cancel - Context cancellation propagates: Client → Middle Server → Backend Server
- Backend server detects cancellation via
context.is_stopped()and stops - All components handle the cancellation gracefully
- Backend Server: Checks
context.is_stopped()before each yield - Middle Server: Forwards requests and passes context through (when used)
- Client: Uses
Context()object and callscontext.stop_generating() - Graceful shutdown: All components handle
asyncio.CancelledError
- The client defaults to direct connection for simplicity
- Use
--middleflag to test the proxy scenario - Both modes demonstrate the same cancellation behavior
- The middle server shows how to properly forward context in proxy scenarios
For more details on the request cancellation architecture, refer to the architecture documentation.