Multi-threading involve running different part of your program concurrently.
For instance you want a certain part of your program which can be either a function or method to keep running without affecting the rest of the program we usually use Multi-threading
In python there several modules which we can use to achieve multi-threading.
On this tutorial I will show you how to conduct multi-threading in Python using threading module
threading module it comes by default with your standard library so you don’t need to install anything
below is a simple app that explore the concept of multi-threading it run the breath function and live function separately without freezing the program.
import time import threading alive = True def breath(): while alive: print('heart beating ... ') time.sleep(1) def live(): while alive: print('struggle ... ') time.sleep(2) breath_t = threading.Thread(target=breath); breath_t.daemon =True live_t = threading.Thread(target=live); live_t.daemon = True breath_t.start() live_t.start() input()
When you run the above program it will run the breath and live function as if the were entirely different programs
The out might look like shown below because it output is affected by processor scheduling which varies on different Architecture
heart beating ... heart beating ... struggle ... heart beating ... heart beating ... struggle ... ......
To get the whole code check it on My Github Profile
In case of any comment or suggestion drop it in the comment box below