Meetup in a Box - Build Invincible Apps with Durable Execution - Python
Welcome to the Temporal Meetup in a Box program!
We're thrilled that you want to share Temporal within your community, and we hope these resources will aid you in doing just that.
This presentation is designed to introduce Python developers of all experience levels to Temporal.
In this presentation, you will lead the audience through the fundamental building blocks of Temporal, including the Temporal Service, Workflows, Activities, Workers, Durable Timers, and Task Queues.
You will explore a distributed Hello World example that uses an external service to obtain your IP address and then uses the result as input to a geo-location service, returning a greeting that provides your IP address and where you currently are.
Along the way, you'll demonstrate various failure scenarios, and how Temporal handles them by providing Durable Execution.
Thank you for your interest in Temporal, we hope you enjoy this Meetup in a Box!
About this Repository
This repository contains the sample application for the Build Invincible Apps with Durable Execution - Python presentation for Meetup in a box.
For more information about this presentation, visit the presentations landing page.
Installation
In order to present this material, you will need to install a development version of the Temporal Service, the Temporal Python SDK, and various packages onto your presentation device.
- Install the Temporal CLI on your local machine by following this tutorial
- Clone this repository to your local machine:
git clone https://github.com/temporal-community/miab-build-invincible-apps-python.git
- Change directories into the cloned repository:
cd miab-build-invincible-apps-python
- Create a Python virtual environment with
venv:python3 -m venv venv
- Activate the virtual environment:
source venv/bin/activate
- Install the required packages, which includes
temporalio, requests, and flask:python -m pip install -r requirements.txt
Teaser Demo
This demo is a quick, 1 - 2 minute teaser of Durable Execution.
It should not be used to introduce any Temporal Concepts yet, that will come later in the presentation.
This demo will quickly demonstrate the difference between "normal" and durable execution.
Normal execution is any execution that is not durable, so it is synonymous with the majority of code that is executed by most developers on a daily basis.
This demo will run a loop that counts from 1 to 10, sleeping for 1 second between each iteration, and logging the value to the screen.
After the loop progresses a bit (about 4 or 5), the loop will be terminated.
In a normal execution, restarting this process will cause the loop to start over from the beginning, but in a durable process we'll see that the execution resumes where it was, continuing the execution.
Running the demo
The sample application requires three terminal windows and a browser to run.
You can either open three separate terminals, or use a terminal multiplex such as screen or tmux to manage your terminals.
In the first terminal, run the following command to start the Temporal Service on port 8080 with a persistent database:
temporal server start-dev --ui-port 8080 --db-filename temporal.db
You should see an output similar to this (the exact output and versions may differ)
CLI 1.1.1 (Server 1.25.1, UI 2.31.2)
Server: localhost:7233
UI: http://localhost:8080
Metrics: http://localhost:65134/metrics
In the second terminal, run the following commands to demo Normal Execution.
The Temporal Service you started will not be used in this portion.
Be sure to activate your virtual environment:
source venv/bin/activate
Change directories into the durable-vs-normal-execution/normal directory:
cd durable-vs-normal-execution/normal
Show the code to the audience, demonstrating it's a standard loop. Then run the Normal Execution script
python counter.py
The script should log the numbers 1 to 10 one at a time, sleeping for 1 second in between, similar to this:
0
1
2
3
4
Once it has gotten to 4 or 5, use CTRL-C and kill the execution.
Ask the audience
What will happen if I run the execution again?
The answer should be "It will start over."
You can ask
Who thinks it will resume where it left off?"
No one will probably think this, as this is not typical behavior for any programming language.
Run the Normal Execution script again
python counter.py
Confirm that the code did, in fact, start over.
For this demo you will need both the second and third terminal. Now you will demonstrate the same application, a loop that counts from 1 to 10, sleeping between each step, that does survive killing the process. When the execution is brought back online, it will resume where it left off.
Remember, this is not the time to go deep into the architecture of Temporal.
Mention that for this sample, there's a running process that executes the code and another script that invokes it.
Open up a terminal and activate the virtual environment
source venv/bin/activate
Change directories into the durable-vs-normal-execution/durable directory:
cd durable-vs-normal-execution/durable
Show the counter.py file to the audience.
Explain that it's a slight modification, but relatively similar.
You added a function to do the addition, and added a few decorators.
DON'T go into depth here, all the terms will be defined later.
Start the Temporal Worker:
python worker.py
You should see output similar to this:
Starting the worker....<PROCESS_ID>@<YOUR_DEVICE_NAME>
Change to another terminal and activate the virtual environment
source venv/bin/activate
Run the starter script, which will begin the execution of the loop code
python starter.py
This code will exit immediately. Return to the other terminal.
In the previous terminal you should see the output of the loop. Once the loop gets to 4 or 5, CTRL-C the process and kill it. You will see extra output:
^CWorker cancelled, shutting down
Beginning worker shutdown, will wait 0:00:00 before cancelling activities
For now you can ignore these.
Ask the same question from the previous demo:
What will happen if I run the execution again?
See if the audience answers differently. But then add:
Wouldn't it be great if it did just resume working?
Restart the Worker and let the code complete
python worker.py
The code should pick up where it left off, resuming from the next number in the count.
Open a browser tab to http://127.0.0.1:8080 to view the Temporal Web UI. Go to the Workflow Execution of this run and show the timeline view. You should see the timers and the call to the add_one method. Click on a single Activity Task and show the input and output.
People may be very interested and start asking a lot of questions, try to hold them off, there will be plenty of time going forward. Go back to the slides and resume the presentation.
Temporal Demos
Below are the instructions to the demos to showcase various features of Temporal using the distributed Hello World application.
Before doing these demos, ensure that you are connected to a network.
These demos are listed in a chronological order, meaning they are designed to be done one after the other, each one building upon the previous demo.
While presenting, feel free to elaborate within these demos.
Personal anecdotes and experiences make the presentation more relatable to the audience.
You will be entering the name of a person into the form multiple times, so to encourage participation, use audience member's' names in the form as input.
While you are demoing, narrate what you are doing as you are doing it.
The more you speak, the more engaged the audience will be.
We have provided scripts in areas below if you need them, but feel free to explain it in your own way if you feel comfortable doing so.
Running the Application
The sample application requires three terminal windows and a browser to run.
You can either open three separate terminals, or use a terminal multiplex such as screen or tmux to manage your terminals.
- In the first terminal, run the following command to start the Temporal Service on port 8080 with a persistent database:
temporal server start-dev --ui-port 8080 --db-filename temporal.db
You should see an output similar to this (the exact output and versions may differ)CLI 1.1.1 (Server 1.25.1, UI 2.31.2)
Server: localhost:7233
UI: http://localhost:8080
Metrics: http://localhost:65134/metrics
- In the second terminal, run the following commands to start the Temporal Worker:
- Be sure to activate your virtual environment:
source venv/bin/activate
- Change directories into the
iplocate directory:cd iplocate
- Start the Temporal Worker:
python worker.py
- In the third terminal, run the following commands to start the Flask application:
- Be sure to activate your virtual environment:
source venv/bin/activate
- Change directories into the
iplocate directory:cd iplocate
- Start the Flask application on port
8000:python app.py
- Open a browser tab to http://127.0.0.1:8000 to view the Flask application.
- Open a browser tab to http://127.0.0.1:8080 to view the Temporal Web UI.
Demo #1 Successful Execution
Purpose: Show the working application
Failure: None
Temporal Feature Demonstrated: Temporal Web UI
Audience Takeaway: Temporal keeps track of the results of each Activity and Workflow.
We can use the Web UI to debug issues when they go wrong.
- Prepare the audience with an introduction to the demo:
"First, let's see the application work successfully. This application is a distributed take on a Hello World program. It takes a user's name, and makes HTTP calls to two different services. The first service retrieves the IP address of the requester and returns it, and the second service takes an IP address and uses it to perform a geo-lookup. This means that the result of the first request will be used as the input for the second request. Once both requests have completed, the application will return a greeting, saying hello, your IP address, and what city you are currently in."
- Enter your name in the text field Enter your name in the web application and press the Get Greeting button.
- The app will return a greeting immediately similar to the following:
Hello, Ziggy!
Your IP Address is 256.256.256.256.
You are in Austin, Texas, United States
- Open the Web UI and walk through the Event History, highlighting the following things:
- The input and output to the Workflow
- The input and output to each Activity, noting how the input to the
get_location_info Activity was the output from the get_ip Activity.
- The timeline view
- Anything else you find interesting that you wish to point out, or any anecdotes on how you use the Web UI
- Return to the Flask application and click the Reset button.
Demo #2 Successful Execution with a Durable Timer
Purpose: Show the working application with a small pause happening between the service calls
Failure: None
Temporal Feature Demonstrated: Durable Timers
Audience Takeaway: Temporal supports Timers. This is setting up for the next demo and allowing us to create time to simulate failures.
- Explain to the audience that next you'll introduce a delay in between the two calls:
"Next, I'm going to introduce a delay between the two calls. This is going to help me simulate various failure scenarios going forward, and also demonstrate the concept of Durable Timers. So this application can take in an integer via the form and sleep for that amount of seconds after executing the first Activity but before executing the second."
- Enter an audience member's name in the text field Enter your name in the web application.
- Press the Show Demo Options link on the page.
- In the Sleep Duration (seconds) section of the form, provide 3 in the Number of Seconds field and press the Get Greeting button.
- The greeting should return after a short delay (3 seconds) similar to the original:
Hello, Ziggy!
Your IP Address is 256.256.256.256.
You are in Austin, Texas, United States
- Open the Web UI and investigate in the Event History. Locate the Timer Started and Timer Fired events
- Tell the audience that Timers are managed on the Temporal Service side, and will fire regardless if there is a Worker running or not.
If a Timer fires and a Worker is not available, it will pick up when a Worker becomes available again.
- Return to the Flask application and click the Reset button.
Demo #3 Worker Becomes Unavailable During an Execution
Purpose: Demonstrate that a Timer fires regardless if there is a Worker running.
Failure