Building a dynamic workflow with custom activities in Temporal (Python)

Hi everyone,

I’m looking to build a dynamic workflow in Temporal using Python that can execute user-defined tasks. My goal is to allow users to specify operations like addition and multiplication on numbers, and have the workflow execute these operations dynamically.

I’ve already set up two separate Python projects:

add_activity: defines an activity function for adding two numbers.

in one project i have:

from temporalio import activity

@activity.defn
async def add(ctx, x: int, y: int) -> int:
  """Adds two numbers and returns the result."""
  result = x + y
  return result
multiply_activity: defines an activity function for multiplying two numbers.in another project I have: 
from temporalio import activity

@activity.defn
async def multiply(ctx, x: int, y: int) -> int:
  """Multiplies two numbers and returns the result."""
  result = x * y
  return result

Now, I want to create a separate workflow project that:

Takes input as two numbers and another number for multiplication.
Uses the add_activity to add the first two numbers.
Uses the multiply_activity to multiply the sum by the third number.
Returns the final result.
from temporalio import activity

@activity.workflow
async def add_and_multiply(ctx, x: int, y: int) -> int:
  """Adds two numbers, multiplies the result by another number, and returns the final result."""
  sum = await ctx.call_activity("add_activity", "add", x, y)
  result = await ctx.call_activity("multiply_activity", "multiply", sum, y)
  return result

All these three projects are in different python projects.Now I want to register these two activities and one workflow with Temporal and then run workflow.

I’m looking for guidance on how to achieve this:

Is this approach feasible with Temporal and Python?
How can I structure the workflow project to achieve the desired functionality?
Are there any best practices or recommendations for building such workflows in Temporal?

I am looking for something like:


    temporal register -p add_activity
    temporal register -p multiply_activity
    temporal register -p workflow_project

    temporal start -w workflow_project add_and_multiply -i '{"x": 5, "y": 3}'

I’ve explored the Temporal documentation and examples, but I haven’t found a clear solution for dynamically composing workflows with user-defined tasks. Any help or insights would be greatly appreciated!

Temporal doesn’t run application code. So you cannot “register” some code with Temporal and then ask it to run it.

Temporal workflows and activities are part of your service/application, and you are responsible for running the process that hosts them.