You are going to build your own Temporal application in Python! But first, as a prerequisite, let's run through a simple application that prints "Hello, Temporal!". Follow the steps below to run the Hello World sample.
We want to minimize the barriers for learning Temporal, and we know that not everyone is able or willing to install software on their computers for a training course. That's why we use the GitPod service to provide a browser-based exercise environment for you to use.
The only thing you'll need besides an up-to-date Web browser is a GitHub account that you can log into from that browser, which allows the GitPod service to set up the exercise code you'll use during the course.
Launch the exercise environment here.
Before you begin, you need to know how to navigate to certain files using git.
- Listing Files in a Directory: In your terminal window, enter
ls
to list the contents of the current folder that you are in. Go ahead and try it now! You should see the directory files listed such asREADME.md
,hello-world
,create-your-app
. - Entering a Folder: To go into a folder, type
cd
followed by the folder name. For example, if you docd hello-world
, you will now enter thehello-world
folder.- Try it now and you will see that you have entered the
hello-world
directory. - Type
ls
again to list all the files in yourhello-world
folder.
- Try it now and you will see that you have entered the
- Going Backwards: Enter
cd ..
to navigate back out to the previous directory. Try it now, and you will see you are back in the root directory.
- For the exercises, make sure to run
temporal server start-dev --ui-port 8080 --db-filename clusterdata.db
in one terminal window to start the Temporal server. - Add another terminal window in addition the one running the Temporal server. You can do this by right clicking a terminal window and click
New Terminal
. - All Python libraries for this course should be installed in a virtual environment. If you are running these exercises in the course's GitPod environment, there is a virtual environment already setup for you and you can skip this section. If you are running these exercises locally, be sure you are using Python 3.7+.
- Run the following command to create a virtual environment:
python3 -m venv env
. - Activate the virtual environment:
- Linux/Mac:
source env/bin/activate
- Windows:
env/Scripts/activate
Once the environment is active you should see (env) prepended to your prompt similar to below:(env) $
- Linux/Mac:
- Install the Temporal SDK:
python -m pip install temporalio
. For every new terminal you open, you will need to activate the environment using the following command:- Linux/Mac:
source env/bin/activate
- Windows:
env/Scripts/activate
- Linux/Mac:
- Run the following command to create a virtual environment:
- Navigate into the
hello-world
directory. - Now, you will start a Temporal Worker. In a terminal window separate from running the server if you have one running, run
python run_worker.py
to start the Worker.- Workers are responsible for running your Temporal code. When you start a Workflow in Temporal, tasks get placed into a Task Queue. The Task Queue helps route tasks to the appropriate Worker, which executes the tasks. Workers continuously poll this queue for tasks and execute them. The Workflow doesn't proceed until a Worker picks up and processes the Workflow Task from the Task Queue.
- Now, you will run a Workflow. You will need to do this in a separate terminal window. To add more terminal windows in Gitpod, right click a terminal window and click
New Terminal
. - To run the Workflow, run
python client.py
.- In the terminal window, you should see:
Hello, Temporal!
. - A Workflow is a sequence of steps defined by writing code, known as a Workflow Definition. These steps are executed as a Workflow Execution. A Workflow Definition is essentially a function, which can store state and orchestrates the execution of Activities. Workflows manage the coordination and logic of your application's processes, while Activities perform the tasks which interact with external services. As mentioned, the Workflow will orchestrate the sequence of Activities such as sending welcome emails, charging customers, and handling cancellations.
- Activities are the building blocks of a Temporal Workflow. They encapsulate the logic for tasks that interact with external services such as querying a database or calling a third-party API. One of the key benefits of using Activities is their built-in fault tolerance. If an Activity fails, Temporal can automatically retry it until it succeeds or reaches a specified retry limit. This ensures that transient issues, like network glitches or temporary service outages, don't result in data loss or incomplete processes.
- In the terminal window, you should see:
Now that you've run a basic hello world
application, it's time for you to try creating your own application.
- Kill the Worker from the
hello-world
project. In the terminal window where your Worker is running, doCTRL C
, right click the terminal window, and clickKill Terminal
. This terminal window will now be removed. - In the terminal window where your
hello world
Workflow was running, navigate into thecreate-your-app
folder where some boiler code is already provided for you to get started.- Enter
cd ..
to navigate back out to the parent directory. You should now be back in the root directory. - Enter
ls
to see all the directories. You should see acreate-your-app
directory. - Enter the
create-your-app
directory by enteringcd create-your-app
.
- Enter
- Start a Temporal Worker that will listen for any work that needs to be performed by running
python run_worker.py
in the same terminal window. - Now open a new terminal window where you will run your workflow with
python client.py
once you've created your own workflow.
Have fun building!