3.3. Sync call
Let's take a look at the following Python function. It calls two functions in sequential order, each of which would take one second to finish. Then, it sleeps for another 0.5 second before reaching the end.
import time
def task_one():
print("Task 1 Start")
time.sleep(1) # Wait for 1 sec.
print("Task 1 Complete")
def task_two():
print("Task 2 Start")
time.sleep(1) # Wait for 1 sec.
print("Task 2 Complete")
task_one()
task_two()
print("Main Start")
time.sleep(0.5)
print("Main Complete")
# Output:
# Task 1 Start
# Task 1 Complete
# Task 2 Start
# Task 2 Complete
# Main Start
# Main Complete
We use the following sequence diagram to show how the program got executed. In a sequence diagram, each participant (Main, Task 1, and Task 2) is shown as a box at the top with a dashed vertical line, called a lifeline, running downward. Time flows from top to bottom. An arrow pointing right represents a function call, and an arrow pointing left represents a return. A long rectangle on a lifeline indicates the period during which that participant is actively executing.
As you can see, Main calls task_one() and waits. Its lifeline is idle while
Task 1's rectangle is active. Only after Task 1 returns does Main
call task_two(), and again waits for it to finish. Finally, with both tasks
complete, Main runs its own 0.5-second sleep. Notice that at every moment in
time, only one rectangle is active across all three lifelines. This is the
hallmark of sequential execution: the program does exactly one thing at a time.