Background Module
The background module provides the ability to run agents in the background, allowing long-running tasks without blocking the main thread.Installation
Features
- Long-running tasks without blocking
- Task queuing and management
- Progress monitoring and notifications
- Graceful cancellation
Quick Start
Classes
BackgroundRunner
Main class for managing background tasks.Constructor
| Parameter | Type | Default | Description |
|---|---|---|---|
max_workers | int | 4 | Maximum concurrent tasks |
Methods
| Method | Description |
|---|---|
submit(agent, prompt, callback) | Submit a task |
cancel(task_id) | Cancel a running task |
get_status(task_id) | Get task status |
list_tasks() | List all tasks |
shutdown() | Shutdown the runner |
BackgroundTask
Represents a background task.Attributes
| Attribute | Type | Description |
|---|---|---|
id | str | Unique task ID |
agent | Agent | Agent executing the task |
prompt | str | Task prompt |
status | TaskStatus | Current status |
result | Any | Task result (when completed) |
error | str | Error message (if failed) |
created_at | datetime | Creation time |
completed_at | datetime | Completion time |
Methods
| Method | Description |
|---|---|
wait() | Wait for task completion |
cancel() | Cancel the task |
get_progress() | Get progress info |
TaskStatus
Enumeration of task statuses.BackgroundConfig
Configuration for background runner.Usage Examples
Basic Background Task
With Callback
Multiple Tasks
Task Cancellation
Progress Monitoring
Best Practices
- Set appropriate timeouts - Prevent tasks from running indefinitely
- Handle failures - Always provide error callbacks
- Limit concurrency - Don’t overwhelm resources with too many workers
- Clean up - Call
shutdown()when done with the runner - Monitor progress - Track long-running tasks

