Basic Task Definition
Define a task by decorating a function with@hy.task
:
All task parameters must have type hints. Hyrex enforces this requirement to ensure type safety.
Task Configuration
Configure task behavior with decorator options:Configuration Options
- queue: Route tasks to specific worker pools
- max_retries: Number of retry attempts on failure (default: 0)
- timeout_seconds: Maximum execution time before timeout
- priority: Task priority from 1-10 (default: 5)
- retry_backoff: Function to calculate retry delay
Sending Tasks
Queue tasks for asynchronous execution:Task Context
Access metadata about the current task execution:Error Handling
Tasks can raise exceptions to trigger retries:Error Callbacks
Useon_error
to handle errors without affecting retry behavior:
Retry Behavior
- Raising any exception triggers a retry (if retries remain)
- Return a value to complete the task (even with errors)
- Use
retry_backoff
for exponential or custom backoff on_error
callbacks run on each failure but don’t affect retries
Task Results
Track task execution:Best Practices
-
Use Type-Safe Contexts
-
Set Appropriate Timeouts
-
Design for Idempotency
-
Handle Errors Gracefully