Skip to main content
Tasks are the fundamental unit of work in Hyrex. They are Python functions that can be executed asynchronously by workers, with automatic retry handling, monitoring, and durability.

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

Use on_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

  1. Use Type-Safe Contexts
  2. Set Appropriate Timeouts
  3. Design for Idempotency
  4. Handle Errors Gracefully

Next Steps

Workflows

Orchestrate multiple tasks

Queues

Route tasks to worker pools

Retries

Configure retry strategies

API Reference

Detailed task API