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:- Content Extraction - Read and parse source documents
- Classification - Categorize and organize content
- Rule Processing - Apply extraction and validation rules
- Document Generation - Create formatted output
- Post-Processing - Optimize and finalize document
- 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:- Response returned immediately (not waiting for completion)
- HTTP status is 202 (Accepted), not 200 (OK)
- Client receives
task_idfor tracking
3. Processing
The task is processed asynchronously in the background:4. Status Polling
Client polls the status endpoint to track progress:- 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:2. Conservative Polling
Poll less frequently to reduce load:3. Exponential Backoff (Recommended)
Start fast, gradually slow down:Error Handling
Task Failed
If a task fails during processing: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: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:Related Topics
- Document Generation - Generation pipeline details
- Documents API - API endpoints and responses
- Status Polling - Status endpoint documentation