Overview
Streaming execution is ideal for:- Long-running tasks where you want real-time progress
- Interactive code that produces output over time
- Monitoring execution progress
- Building real-time dashboards or UIs
Streaming execution uses WebSocket connections for real-time communication. It requires async/await support in your code.
Basic Streaming
Stream execution output in real-time:- Python
- JavaScript
Message Types
Stream messages have different types:- Python
- JavaScript
Progress Monitoring
Monitor progress of long-running tasks:- Python
- JavaScript
Environment Variables
Pass environment variables to streaming execution:- Python
- JavaScript
Error Handling
Handle errors in streaming execution:- Python
- JavaScript
Complete Example
Hereโs a complete example with real-time progress:- Python
- JavaScript
Best Practices
1
1. Use Async/Await
Streaming execution requires async/await. Make sure your code uses async functions and
async for loops.2
2. Handle All Message Types
Process stdout, stderr, result, and complete messages appropriately for a complete streaming experience.
3
3. Break on Complete
Always break the loop when you receive a โcompleteโ message to avoid unnecessary processing.
4
4. Handle Errors
Check exit codes and success flags to handle execution failures gracefully.
5
5. Use for Real-Time Feedback
Streaming is best for tasks where real-time output is valuable. For simple scripts, synchronous execution may be simpler.
Related
- Synchronous Execution - Basic code execution
- Background Execution - Long-running tasks
- Rich Output Capture - Capture plots and visualizations
- SDK: sandbox.run_code_stream() - Python SDK method
- CLI Code Execution - Execute code from CLI
Next Steps
- Learn about Synchronous Execution for immediate results
- Explore Background Execution for non-blocking tasks
- Review Process Management for monitoring

