MULTI PROCESSING
How to make python do process simultaneously ? Is it Possible?
DO many things at a time? OMNI POTENT?
Python supports multi processing. It utilizes the multi cores and Multi CPU's and speed up the process time.
Process:
- Process is an instance of a program
- Starting a process is slower that starting a thread
- Larger memory footprint
- IPC (inter-process communication) is more complicated
Thread
- A thread is an light weight process within a process that can be scheduled for execution .
- A Process can spawn multiple threads.
- Key facts: - Multiple threads can be spawned within one process. Memory is shared between all threads
- Starting a thread is faster than starting a process - Great for I/O-bound tasks - Light weight - low memory footprint
- One GIL( Global interpreter lock) for all threads, i.e. threads are limited by GIL
- Multithreading has no effect for CPU-bound tasks due to the GIL - Not interruptible/killable -> be careful with memory leaks - increased potential for race conditions
In this, we will take care of Multiprocessing. Multithreading, we will take later. Open PyCharm IDE. Open project. Give any name as project name. In main.py. Type in /copy paste the following code and Run. It is Simple Processing to multiply.
import time
start =time.perf_counter()
def multiplication_table(begin, end):
for i in range(begin, end+1):
for j in range(begin, end+1):
#temp = i * j
print(f'{i:<4}*{j:4} ={i * j:6}')
multiplication_table(1,5000)
finish =time.perf_counter()
print(f'\nSingle Finished in {round(finish-start, 2)}second(s)')
"""
Single Finished in 510.05second(s)
"""
Let us Multiprocessing the same print multiplication operation for P1, 5000 using 5 Processes P1 with 1,1000, P2 with 1001,2000, P3 with 2001 to 3000, P4 with 3001 to 4000, P5 with 4001 to 5000.
import multiprocessing
import time
start=time.perf_counter()
def print_multi(begin, end):
for i in range(begin, end + 1):
for j in range(begin, end + 1):
temp = i * j
print(f'T{i:<4}*{j:4} ={i * j:6}')
if __name__ == "__main__":
p1 = multiprocessing.Process(target=print_multi, args=(1, 1000))
p2 = multiprocessing.Process(target=print_multi, args=(1001, 2000))
p3 = multiprocessing.Process(target=print_multi, args=(2001, 3000))
p4 = multiprocessing.Process(target=print_multi, args=(3001, 4000))
p5 = multiprocessing.Process(target=print_multi, args =(4001, 5000))
p1.start()
p2.start()
p3.start()
p4.start()
p5.start()
p1.join()
p2.join()
p3.join()
p4.join()
p5.join()
# print("Done")
finish = time.perf_counter()
print(f'MULTI in {round(finish-start, 2)} seconds (s)\n')
"""
MULTI in 80.44 seconds (s)
"""
See the difference 510.05 - 80.44 = 429.61 seconds
For Many Processes, You can save Time. You can spend this quality time with your family 🐤🐤🐤
Happy Digital Learning with AMET!!!
Good sir. Thanks. Easy to Understand.
ReplyDelete