Skip to content

[Feature Request] Support executing Workflow directly #891

Open
@Kilo59

Description

@Kilo59

Is your feature request related to a problem? Please describe.

I'm often frustrated when I have some piece business logic as part of a Temporal Workflow that I also want to execute outside of a Temporal Worker. I would like to be able to avoid duplicating this code as much as possible.

Describe the solution you'd like

Being able to call a Workflow.run method directly rather than only being able to run it via a temporal worker is one way to get around this issue.

However the below example will throw a temporalio.workflow._NotInWorkflowEventLoopError: Not in workflow event loop error.

import asyncio
import dataclasses

from temporalio import activity, workflow


@dataclasses.dataclass
class MyArgs:
    to_greet: str


@workflow.defn
class GreetingWorkflow:
    @workflow.run
    async def run(self, arg: MyArgs) -> list[str]:
        log: list[str] = []

        hello = await workflow.execute_activity(
            GreetingActivities.say_hello,
            arg.to_greet,
        )
        print(hello)
        log.append(hello)

        goodbye = await workflow.execute_activity(
            GreetingActivities.say_goodbye,
            arg.to_greet,
        )
        print(goodbye)
        log.append(goodbye)
        return log


class GreetingActivities:
    @activity.defn
    async def say_hello(self, to_greet: str) -> str:
        return f"Hello {to_greet}!"

    @activity.defn
    async def say_goodbye(self, to_greet: str) -> str:
        return f"Goodbye {to_greet}!"


if __name__ == "__main__":
    wf = GreetingWorkflow()
    asyncio.run(wf.run(MyArgs(to_greet="world")))

Additional context

I don't particular care about running the workflow per se, I just would like to avoid duplicating the "activity" orchestration logic in the workflow layer.

Also when run this way I don't expect to retain the standard temporal "durable execution" guarantees.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions