Quick Start
How It Works
| Phase | Description |
|---|---|
| Initialization | Creates async primitives lazily on first use |
| Scheduling | Runs agent at specified intervals with exponential backoff |
| Execution | Uses thread pool for sync agents, direct await for async agents |
| Cancellation | Cooperative cancellation via asyncio.Event |
Configuration Options
AsyncAgentScheduler API Reference
Complete parameter documentation and examples
Constructor Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
agent | Any | Required | Agent instance to schedule |
task | str | Required | Task description to execute |
config | Dict[str, Any] | {} | Optional configuration |
on_success | Callable | None | Success callback (sync or async) |
on_failure | Callable | None | Failure callback (sync or async) |
Start Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
schedule_expr | str | Required | Schedule interval expression |
max_retries | int | 3 | Total attempts (1 initial + retries) |
run_immediately | bool | False | Execute immediately before scheduling |
Common Patterns
Pattern 1: Event Loop Safety
The scheduler is safe to construct outside an event loop:Pattern 2: FastAPI Integration
Pattern 3: Cancellation Handling
Best Practices
Event Loop Safety
Event Loop Safety
Always create async primitives lazily. The scheduler binds
asyncio.Event and asyncio.Lock to the caller’s loop on first async entry, preventing “different loop” errors.Callback Best Practices
Callback Best Practices
Use both sync and async callbacks safely. The
safe_call utility handles both types automatically.Retry Strategy
Retry Strategy
Use exponential backoff with jitter. Both sync and async schedulers share the same
backoff_delay algorithm for consistency.Graceful Shutdown
Graceful Shutdown
Always await
stop() for proper cleanup and statistics reporting.Related
Sync Agent Scheduler
Thread-based scheduler for sync environments
Shared Scheduler Utilities
Common primitives used by both schedulers

