multiprocessing with python code example

Example 1: python multiprocessing

import multiprocessing

def worker1(i):
    print(i, "Process started")

def worker2(i):
    print(i, "Process started")
    
if __name__ == '__main__':
    p = multiprocessing.Process(target=worker1, args("nice",))
    q = multiprocessing.Process(target=worker2, args("nice2",))
    q.start()
    p.start()

Example 2: python - multiprocessing

import time
from multiprocessing import Process

# My functions (threads)
def my_func_1():...
def my_func_2():...

# Single calculation  
start = time.time()
my_func_1()
my_func_2()
print(f'Single thread total time: {time.time() - start}')

# Processes
process = Process(target=my_func_1)
process2 = Process(target=my_func_2)
process.start()
process2.start()

start = time.time() # Start the two processes

process.join()      # Wait till processes finish
process2.join()

print(f'Two thread total time: {time.time() - start}')