Prerequisites
Before you begin, make sure you have:- Active sandbox - A running sandbox (see Creating Sandboxes)
- Understanding of synchronous execution - Familiarity with Synchronous Execution is helpful
- Long-running task - Code that takes more than a few minutes to complete
Overview
Background execution is perfect for:- Long-running computations (>5 minutes)
- Tasks that don’t need immediate results
- Parallel processing of multiple tasks
- Workflows where you want to check status periodically
Background execution returns immediately with a
process_id. Use Process Management to check status and manage background processes.Starting Background Execution
Start code execution in the background:- Python
- JavaScript
Checking Process Status
Check the status of background processes:- Python
- JavaScript
Process Naming
Name your processes for easier identification:- Python
- JavaScript
Environment Variables
Pass environment variables to background execution:- Python
- JavaScript
Timeout Configuration
Set timeout for background execution:- Python
- JavaScript
Complete Example
Here’s a complete example showing background execution workflow:- Python
- JavaScript
Best Practices
1. Use Descriptive Names
Always name your background processes for easier identification when listing and managing them.
2. Set Appropriate Timeouts
Set timeouts based on expected execution time. Default is 5 minutes, but you can extend for longer tasks.
3. Monitor Status Regularly
Check process status periodically to track progress and handle completion or failures.
4. Clean Up Completed Processes
Remove completed processes to keep your process list manageable. Use Process Management for cleanup.
Implementation
- SDK: sandbox.run_code_background() - Python SDK method
- CLI - Command-line interface
- API: POST /execute/background - VM Agent API endpoint
Related
- Process Management - Monitor and manage background processes
- Synchronous Execution - Execute code synchronously
- Streaming Execution - Real-time output streaming
- Async Execution with Webhooks - Very long-running tasks
Next Steps
- Learn about Process Management to monitor and control background processes
- Explore Async Webhooks for very long-running tasks (>30 minutes)
- Review Synchronous Execution for immediate results

