Thursday, April 14, 2022

Parallel Programming

 This demo program show how to run several functions in parallel.


The output:



The Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import multiprocessing as mp
import time 

start_timer = time.perf_counter()

def p_process(v):
    print(v)
    time.sleep(1)


if __name__ == '__main__':
    p = ["This is the function!"] * 10
    processes1 = mp.Pool(processes=10)
    set_proc = processes1.map_async(p_process, p)
    set_proc.wait()
    print ("Next Functions")
    
    finish_timer = time.perf_counter() 
    t_fin = (round(finish_timer-start_timer, 2))
    print('Done in ' + str(t_fin) + ' second(s)') 

No comments:

Post a Comment