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}')
import multiprocessing
# empty list with global scope
result = []
def square_list(mylist):
"""
function to square a given list
"""
global result
# append squares of mylist to global list result
for num in mylist:
result.append(num * num)
# print global list result
print("Result(in process p1): {}".format(result))
if __name__ == "__main__":
# input list
mylist = [1,2,3,4]
# creating new process
p1 = multiprocessing.Process(target=square_list, args=(mylist,))
# starting process
p1.start()
# wait until process is finished
p1.join()
# print global result list
print("Result(in main program): {}".format(result))