Skip to content

Add workflow.in_workflow() utility #799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions temporalio/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,11 @@ def instance() -> Any:
return _Runtime.current().workflow_instance()


def in_workflow() -> bool:
"""Whether the code is currently running in a workflow."""
return _Runtime.maybe_current() is not None


def memo() -> Mapping[str, Any]:
"""Current workflow's memo values, converted without type hints.

Expand Down
21 changes: 21 additions & 0 deletions tests/worker/test_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6981,6 +6981,27 @@ async def test_update_handler_semaphore_acquisition_respects_timeout(
)


def check_in_workflow() -> str:
return "in workflow" if workflow.in_workflow() else "not in workflow"


@workflow.defn
class InWorkflowUtilWorkflow:
@workflow.run
async def run(self) -> str:
return check_in_workflow()


async def test_in_workflow_util(client: Client):
assert check_in_workflow() == "not in workflow"
async with new_worker(client, InWorkflowUtilWorkflow) as worker:
assert "in workflow" == await client.execute_workflow(
InWorkflowUtilWorkflow.run,
id=f"workflow-{uuid.uuid4()}",
task_queue=worker.task_queue,
)


deadlock_interruptible_completed = 0


Expand Down
Loading