Multi Threading and multitasking in Python
Executing many tasks simultaneously at a time is the concept of Multitasking.
Types of Multitasking are-
1.Process-based
2.Thread based
Process-based Multitasking-
When several tasks are executed simultaneously and where each task has a separate independent process is known as process-based multitasking.
Eg: While we do coding in Python in the editor we can listen to songs at the same time from the same system in the background.
Process-based multitasking is the best option for operating system level.
Thread Based Multitasking-
When each task is separate but part of the same program and is executed simultaneously then that type of multitasking is called Thread Based Multitasking. Each independent part is called a Thread.
Thread based multitasking type best at the programmatic level.
Some of the important application areas of this type of threading are-
1. Implement multimedia graphics
2. Develop animations.
3. Developing video games
4. Develop web and application server.
Python helps us providing inbuilt module “threading” to provide support for developing threads. So, developing multi-threaded programs are considered as easy in Python.
Creating a Thread in Python –
1. Thread without using any class.
2. Thread by extending Thread class.
3. Thread without extending Thread class.
Creating a Thread without using any class-
from threading import * def display(): for i in range(1,11): print("Child Thread "); t=Thread(target=display) # creating Thread object t.start() for i in range(1,11): print("Main Thread ")
Thread by extending Thread class-
from threading import * class MyThread(Thread): def run(self): for i in range(10): print("Child Thread-1"); t=MyThread() t.start() for i in range(10): print("Main Thread-1")
Creating a Thread without extending Thread class-
from threading import * class Test: def display(self): for i in range(10): print("Child Thread-2"); obj=Test() t=Thread(target=obj.display) t.start() for i in range(10): print("Main Thread-2");
Single Threading-
import threading,time def Test(n): i=1 while i<=n: print("Main Thread =",i); i=i+1 time.sleep(5) if __name__ == "__main__": t1 = threading.Thread(target=Test,args=(5,)) t1.start() t1.join()
Multi-Threading program-
import threading,time def Test(n): i=1 while i<=n: print("Main Thread =",i); i=i+1 time.sleep(5) def Demo(n): i=1 while i<=n: print("Child Thread =",i); i=i+1 time.sleep(5) if __name__ == "__main__": t1 = threading.Thread(target=Test,args=(5,)) t2=threading.Thread(target=Demo,args=(5,)) t1.start() t2.start()
Output-
After every 5 seconds, a new output will be generated.
Main Thread = Child Thread = 11 Main Thread =Child Thread = 22 Main Thread =Child Thread = 33 Main Thread =Child Thread = 44 Main Thread =Child Thread = 55
Timer program-
import time, threading def foo(): print(time.ctime()) threading.Timer(1, foo).start() foo()
Example 2-
from threading import Timer from time import sleep def hello(): print ("hello, world") t = Timer(3,hello) t.start() t = Timer(3, hello) t.start() # after 3 seconds, "hello, world" will be printed # timer will wake up ever 3 seconds, while we do something else while True: print ("do something else") sleep(10)
Output-
do something else hello, world hello, world hello, world hello, world do something else hello, world hello, world hello, world do something else hello, world hello, world hello, world hello, world
and it will continue further.
Leave a Reply