Use durable queues for background tasks
mainDBOS queues allow you to run tasks (single steps or entire workflows) in the background using Postgres as the backend. DBOS guarantees task completion and result retrieval even if the application is interrupted.
Key capabilities include:
- Flow control (limiting concurrency per queue or process).
- Task timeouts and rate limiting.
- Task deduplication and prioritization.
To use a queue, instantiate a Queue object and use queue.enqueue(function, args) to add tasks. You can then use the returned handle to retrieve results via handle.get_result().
from dbos import DBOS, Queue
queue = Queue("example_queue")
@DBOS.step()
def process_task(task):
...
@DBOS.workflow()
def process_tasks(tasks):
task_handles = []
# Enqueue each task so all tasks are processed concurrently.
for task in tasks:
handle = queue.enqueue(process_task, task)
task_handles.append(handle)
# Wait for each task to complete and retrieve its result.
# Return the results of all tasks.
return [handle.get_result() for handle in task_handles]