Overview

Some APIs return an async_result_id. While this is useful if you want to continue the workflow while it is running, sometimes you want to wait for the operation to complete. This is where the suspend_fn method comes in.

Suspending a Workflow

To suspend a workflow, you can use the ctx.suspend_fn method within a Step function. This will suspend the workflow until a resume event is received.

The standard way to trigger a resume event when a async_result_id is ready is to use the resume_when_complete method.

from workflows_py.workflow import ScreenStep, Step, Workflow
from client_manager import SampleHealthcareClient

def reasoning_sync(documents, task):
    if ctx.resume_data:
        return ctx.resume_data

    client = SampleHealthcareClient()
    async_result_id = client.v2.documents.legacy.reason(documents=documents, task=task).async_result_id
    
    client.v2.workflow_run.resume_when_complete(
        async_result_id=async_result_id,
    )

    ctx.suspend_fn({"type": "generate-document", "asyncResultId": async_result_id})

def generate_document(ctx):
    ...

workflow = Workflow()
workflow.then(
    Step("reasoning", reasoning_sync)
).then(
    Step("generate-document", generate_document)
)

Note that the same function (reasoning_sync) will be called when the workflow is resumed, and the resume data will be passed via the ctx.resume_data property (in this case, the result from the async_result_id).