Skip to main content

Async Operations

Artos uses asynchronous processing for long-running tasks like document generation. This guide explains how async operations work and best practices for handling them.

Why Async Operations?

Document generation involves multiple time-consuming steps:
  1. Content Extraction - Read and parse source documents
  2. Classification - Categorize and organize content
  3. Rule Processing - Apply extraction and validation rules
  4. Document Generation - Create formatted output
  5. Post-Processing - Optimize and finalize document
This can take several minutes to complete, making synchronous processing impractical for web APIs. Async operations enable:
  • Immediate Response - Client gets task ID immediately
  • Non-Blocking - Client can continue other work
  • Progress Tracking - Poll status at any time
  • Scalability - Server can process multiple requests
  • Reliability - Failed tasks can be retried

How Async Works in Artos

1. Request Submitted

Client submits document generation request:

2. Task Queued (202 Accepted)

Request is accepted and queued as a Celery task:
Key Points:
  • Response returned immediately (not waiting for completion)
  • HTTP status is 202 (Accepted), not 200 (OK)
  • Client receives task_id for tracking

3. Processing

The task is processed asynchronously in the background:
Client doesn’t wait - they can continue immediately.

4. Status Polling

Client polls the status endpoint to track progress:
Response includes current status:
Status Values:
  • Generating - Task is currently processing
  • Complete - Task finished successfully
  • Failed - Task encountered an error

5. Result Retrieval

Once complete, retrieve the document:

Implementation Pattern

Basic Polling Pattern

Polling with Exponential Backoff

Optimize polling by increasing wait time:

Async/Await Pattern

For async Python code:

Polling Strategies

1. Aggressive Polling

Poll frequently for immediate feedback:
Pros: Fast feedback Cons: Higher server load, more requests

2. Conservative Polling

Poll less frequently to reduce load:
Pros: Lower server load Cons: Slower feedback Start fast, gradually slow down:
Pros: Good balance of responsiveness and load Cons: More complex code

Error Handling

Task Failed

If a task fails during processing:
Handle failures:

Timeout

Task takes longer than expected:

Webhook Alternative (Future)

Currently polling is the only way to track status. In the future, Artos may support webhooks:
Until webhooks are available, use polling.

Best Practices

1. Implement Proper Timeout

Always set a reasonable timeout:

2. Use Exponential Backoff

Start fast, slow down over time:

3. Handle Errors Gracefully

Always handle failure cases:

4. Log Task IDs

Keep task IDs for debugging and support:

5. Cache Status

Avoid polling the same task multiple times: