For a complete working example of streaming with multi-GPU inference, see the Parallel SDXL Tutorial.
How Streaming Works
Withfal.distributed, you can stream results from workers during execution:
Basic Streaming Example
1. Stream from Workers
In yourDistributedWorker, use add_streaming_result() to send intermediate results:
add_streaming_result(): Sends data to the clientas_text_event=True: Formats as Server-Sent Events (SSE)- Only rank 0 should stream to avoid duplicate messages
2. Create Streaming Endpoint
Define an endpoint that returns aStreamingResponse:
3. Consume Stream from Client
JavaScript/TypeScript:If your endpoint uses a path other than
/stream, specify it with the path parameter to match your @fal.endpoint() decorator.Advanced: Streaming with Gather
Stream intermediate results from all GPUs and combine them:Best Practices
1. Stream Only from Rank 0
Avoid duplicate messages by only streaming from the main worker:2. Throttle Stream Frequency
Don’t stream on every iteration - use intervals:3. Use Synchronization
Synchronize workers after streaming to maintain consistency:4. Keep Payloads Small
Stream minimal data for responsiveness:5. Handle Images Efficiently
For streaming images, use base64 encoding:Complete Example
See the Multi-GPU Inference Tutorial for a complete working example with streaming, including:- Real-time preview generation
- Progress updates every 5 steps
- Gathering results from multiple GPUs
- Progressive blur effects during generation
Next Steps
Multi-GPU Inference Tutorial
Complete streaming example with SDXL
Real-time Endpoints
Learn about fal’s real-time framework