multiprocessing and multithreading in python code example
Example 1: python threading vs multiprocessing
The Python threading module uses threads instead of processes.
Threads uniquely run in the same unique memory heap.
Whereas Processes run in separate memory heaps.
This makes sharing information harder with processes and object instances.
One problem arises because threads use the same memory heap,
multiple threads can write to the same location in the memory heap
which is why the global interpreter lock(GIL) in CPython was created
as a mutex to prevent it from happening.
Example 2: python - multiprocessing
import time
from multiprocessing import Process
def my_func_1():...
def my_func_2():...
start = time.time()
my_func_1()
my_func_2()
print(f'Single thread total time: {time.time() - start}')
process = Process(target=my_func_1)
process2 = Process(target=my_func_2)
process.start()
process2.start()
start = time.time()
process.join()
process2.join()
print(f'Two thread total time: {time.time() - start}')