Files
antigravity-skills-reference/web-app/public/skills/dbos-python/references/workflow-background.md

1.3 KiB

title, impact, impactDescription, tags
title impact impactDescription tags
Start Workflows in Background CRITICAL Background workflows survive crashes and restarts workflow, background, start_workflow, handle

Start Workflows in Background

Use DBOS.start_workflow to run workflows in the background. This returns a handle to monitor or retrieve results.

Incorrect (using threads):

import threading

@DBOS.workflow()
def long_task(data):
    # Long running work
    pass

# Don't use threads for DBOS workflows!
thread = threading.Thread(target=long_task, args=(data,))
thread.start()

Correct (using start_workflow):

from dbos import DBOS, WorkflowHandle

@DBOS.workflow()
def long_task(data):
    # Long running work
    return "done"

# Start workflow in background
handle: WorkflowHandle = DBOS.start_workflow(long_task, data)

# Later, get the result
result = handle.get_result()

# Or check status
status = handle.get_status()

You can retrieve a workflow handle later using its ID:

# Get workflow ID
workflow_id = handle.get_workflow_id()

# Later, retrieve the handle
handle = DBOS.retrieve_workflow(workflow_id)
result = handle.get_result()

Reference: Starting Workflows