Replies: 1 comment
-
Hi @jinhu2659! I'm Dosu and I’m helping the llama_index team. LlamaIndex does not automatically propagate cancellation from a parent workflow to nested sub-workflows. When you call Recommended approach: @step
async def parent_step(self, ev: StartEvent) -> StopEvent:
print("SupWorkflow: Starting parent step")
sub_handle = self.sub_workflow.run()
# Optionally, stream events from the sub-workflow here
result = await sub_handle
print(f"SupWorkflow: SubWorkflow result: {result}")
return StopEvent(result="Parent workflow done") Then, in your cancellation logic, make sure to cancel both handles: parent_handle = parent_workflow.run()
sub_handle = parent_workflow.sub_workflow.run()
await asyncio.sleep(3)
print("Attempting to cancel parent workflow and sub-workflow")
await parent_handle.cancel_run()
await sub_handle.cancel_run() Alternatively, you can create an async task that monitors the parent workflow's cancellation and propagates it to the sub-workflow [source]. Best practices:
Note: For more details and examples, see the discussion here and this issue. To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Context:
I have a parent workflow (
SupWorkflow
) that initializes a sub-workflow (SubWorkflow
) in its__init__
method. The sub-workflow is executed as part of a step in the parent workflow. When I callcancel_run()
on the handle of the parent workflow, the sub-workflow continues running, which is unexpected. I would like guidance on how to properly cancel both the parent and nested workflows to ensure the sub-workflow stops execution as well.Steps to Reproduce:
SupWorkflow
) that initializes a sub-workflow (SubWorkflow
) in its__init__
method.cancel_run()
on its handle.Code Example:
Expected Behavior:
When
cancel_run()
is called on the parent workflow's handle, both the parent workflow (SupWorkflow
) and the nested sub-workflow (SubWorkflow
) should stop execution immediately.Actual Behavior:
The parent workflow stops, but the sub-workflow continues running, as evidenced by the continued printing of
SubWorkflow: Processing ...
messages after cancellation.Question:
cancel_run()
on the parent workflow also cancels the nested sub-workflow?Environment:
Additional Notes:
Beta Was this translation helpful? Give feedback.
All reactions