In this post I will try to give you some insights in my mental model about parallelism when using asynchronous or threading style parallelism. And I also show how you can utilize both types simultaneous.
Disclaimer
This may not be a super valid article about parallelism in Python. It may also not be an proper academic definition of things I write here, but it’ll address some very basic understanding about how to write proper asynchronous code.
You will learn how to run async code at a next level leaving old design patterns from times of threading.
I also recommend this read to async beginners to avoid doing things wrong at the first step, because in many cases you will not recognized that there is a better way.
Synchronous: Synchronous but Vertical
Synchronous programming means that the control flow of the program runs synchronous. Each line of code is executed instruction by instruction and line by line from to bottom allowing only jumps at well defined instructions. Apart from well defined jumps the code is executed vertically.
# synchron code
def foo():
print("starting here")
a = 0
a += 1
b = (a + 1) / 2
# this for loop jumps at a well-defined place.
for x in range(10):
b += x
print("leaving funtion")
return b
Code goes from top to bottom.
Jumps occur here at the for loop
Code is executed vertically
For sure this nothing new to you but for the next thoughts it could be useful to keep this in mind.