1"""A very simple parallel code example to execute parallel functions in python"""
2import multiprocessing
3import numpy as np
4def multiprocessing_func(x):
5 """Individually prints the squares y_i of the elements x_i of a vector x"""
6 for x_i in x:
7 y=x_i**2
8 print('The square of ',x_i,' is ',y)
9def chunks(input, n):
10 """Yields successive n-sized chunks of input"""
11 for i in range(0, len(input), n):
12 yield input[i:i + n]
13if __name__=='__main__':
14 n_proc=4 #Numer of available processors
15 x=np.arange(100) #Input
16 chunked_x=list(chunks(x, int(x.shape[0]/n_proc)+1)) #Splits input among n_proc chunks
17 processes=[] #Initialize the parallel processes list
18 for i in np.arange(0,n_proc):
19 """Execute the target function on the n_proc target processors using the splitted input"""
20 p = multiprocessing.Process(target=multiprocessing_func,args=(chunked_x[i],))
21 processes.append(p)
22 p.start()
23 for process in processes:
24 process.join()
1from multiprocessing import Process
2
3def say_hello(name='world'):
4 print "Hello, %s" % name
5
6p = Process(target=say_hello)
7p.start()
8p.join() # Tells the program to wait until p has finished it's job before exiting
9
1# Parallelizing using Pool.apply()
2
3import multiprocessing as mp
4
5# Step 1: Init multiprocessing.Pool()
6pool = mp.Pool(mp.cpu_count())
7
8# Step 2: `pool.apply` the `howmany_within_range()`
9results = [pool.apply(howmany_within_range, args=(row, 4, 8)) for row in data]
10
11# Step 3: Don't forget to close
12pool.close()
13
14print(results[:10])
15#> [3, 1, 4, 4, 4, 2, 1, 1, 3, 3]