Skip to main content
Temporal Python SDK

Run the Temporal Application

~5 minutesTemporal beginnerHands-on tutorial
  1. Build the application
  2. Test and run a Worker
  3. Run the application

With your Workflow, Activity, test, and Worker in place, the final piece is starting the Workflow from a small client program. You'll then run the Worker and the client side-by-side to see your Temporal Application execute end-to-end.

Write code to start a Workflow Execution

You can start a Workflow Execution by using the Temporal CLI or by writing code using the Temporal SDK. In this tutorial, you'll use the Temporal SDK to start the Workflow, which is how most real-world applications work.

Starting a Workflow Execution using the Temporal SDK involves connecting to the Temporal Server, configuring the Task Queue the Workflow should use, and starting the Workflow with the input parameters it expects. In a real application, you may invoke this code when someone submits a form, presses a button, or visits a certain URL. In this tutorial, you'll create a small command-line program that starts the Workflow Execution.

Create the file run_workflow.py and add the following to connect to the server and start the Workflow:

run_workflow.py
import asyncio

from run_worker import SayHello
from temporalio.client import Client


async def main():
# Create client connected to server at the given address
client = await Client.connect("localhost:7233")

# Execute a workflow
result = await client.execute_workflow(
SayHello.run, "Temporal", id="hello-workflow", task_queue="hello-task-queue"
)

print(f"Result: {result}")


if __name__ == "__main__":
asyncio.run(main())

Like the Worker you created, this program uses client.Connect to connect to the Temporal server. It then executes the Workflow using client.ExecuteWorkflow, which requires the Workflow to run, the input parameters for the Workflow, a Workflow ID for the Workflow, and the Task Queue to use. The Worker you configured is looking for tasks on that Task Queue.

Specify a Workflow ID

You need to specify a Workflow ID when executing a Workflow. You'll use this ID to locate the Workflow Execution later in logs or to interact with a running Workflow in the future.

Using a Workflow ID that reflects some business process or entity is a good practice. For example, you might use a customer identifier or email address as part of the Workflow ID if you ran one Workflow per customer. This would make it easier to find all the Workflow Executions related to that customer later.

You have a Workflow, an Activity, a Worker, and a way to start a Workflow Execution. It's time to run the Workflow.

Run the Temporal Application

To run your Temporal Application, you need to start the Workflow and the Worker. You can start these in any order, but you'll need to run each command from a separate terminal window, as the Worker needs to be constantly running to look for tasks to execute.

First, ensure that your local Temporal Cluster is running. If it is not running, run the following command to start it.

temporal server start-dev

To start the Worker, run this command from the project root:

python run_worker.py

You won't see any output right away, but leave the program running.

To start the Workflow, open a new terminal window and switch to your project root:

cd hello-world-temporal

Activate the virtual environment in this terminal:

env\Scripts\activate

Then run run_workflow.py from the project root to start the Workflow Execution:

python run_workflow.py

The program runs and returns the result:

Result: Hello, Temporal!

Switch to the terminal window that's running the Worker. Stop the Worker process with CTRL-C in the terminal.

You have successfully built a Temporal Application from scratch.

Conclusion

You now know how to build a Temporal Application using the Python SDK. You've built a Workflow, an Activity, a test suite, and a Worker. Along the way, you saw how Temporal's building blocks fit together: the Workflow orchestrates the Activity, the Worker hosts and executes the code, and a client program kicks off the Workflow Execution.

Get notified when we launch new educational content

New courses, tutorials, and learning resources - straight to your inbox.

Subscribe
Feedback