Skip to content

Commit dfc6e7f

Browse files
authored
Add workflow.in_workflow() utility (#799)
Fixes #415
1 parent 7ffa822 commit dfc6e7f

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

temporalio/workflow.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,11 @@ def instance() -> Any:
844844
return _Runtime.current().workflow_instance()
845845

846846

847+
def in_workflow() -> bool:
848+
"""Whether the code is currently running in a workflow."""
849+
return _Runtime.maybe_current() is not None
850+
851+
847852
def memo() -> Mapping[str, Any]:
848853
"""Current workflow's memo values, converted without type hints.
849854

tests/worker/test_workflow.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6981,6 +6981,27 @@ async def test_update_handler_semaphore_acquisition_respects_timeout(
69816981
)
69826982

69836983

6984+
def check_in_workflow() -> str:
6985+
return "in workflow" if workflow.in_workflow() else "not in workflow"
6986+
6987+
6988+
@workflow.defn
6989+
class InWorkflowUtilWorkflow:
6990+
@workflow.run
6991+
async def run(self) -> str:
6992+
return check_in_workflow()
6993+
6994+
6995+
async def test_in_workflow_util(client: Client):
6996+
assert check_in_workflow() == "not in workflow"
6997+
async with new_worker(client, InWorkflowUtilWorkflow) as worker:
6998+
assert "in workflow" == await client.execute_workflow(
6999+
InWorkflowUtilWorkflow.run,
7000+
id=f"workflow-{uuid.uuid4()}",
7001+
task_queue=worker.task_queue,
7002+
)
7003+
7004+
69847005
deadlock_interruptible_completed = 0
69857006

69867007

0 commit comments

Comments
 (0)