Posts

Showing posts with the label UDP

Introduction to multitasking with Python #001 multithreading (2020 tutorial)

Image
1. What is multitasking? So far, we have written so many programs with python. All the instructions inside a program run sequentially one by one. Let's look at an simple example: # multithreading_example01.py # basci program without using threading import time def sing (): for i in range ( 5 ): print ( 'Singing...' ) time . sleep( 1 ) def dance (): for i in range ( 5 ): print ( 'Dancing...' ) time . sleep( 1 ) def main (): # singing a song sing() # dancing dance() if __name__ == '__main__' : main()  After running this program, after ten seconds the program exits. But if we want to dance and sing at the same time intead of doing one after another, what we could do with the code? In python, we could use the threading module and run both sing() and dance functions at the same time. Here is how we can do it: # multithreading_example01.py