forked from pulkitag/pyphy-engine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
try_parallel.py
48 lines (41 loc) · 894 Bytes
/
try_parallel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import numpy as np
from multiprocessing import Pool, Process
import time
def f(x):
time.sleep(2)
return x*x
def rand():
return np.random.random()
def run_map(numW=4, numJobs=20):
t1= time.time()
pool = Pool(numW)
print(pool.map(f, range(numJobs)))
t2 = time.time()
print('Time: ', t2 - t1)
def run_process(numJobs=20):
t1= time.time()
prcs = []
for i in range(numJobs):
p = Process(target=f, args=(i,))
p.start()
prcs.append(p)
print('All Processes launched')
for p in prcs:
p.join()
t2= time.time()
print('Time: ', t2 - t1)
def run_async(numW=4, numJobs=20):
t1= time.time()
p = Pool(numW)
resPool = []
for i in range(numJobs):
#resPool.append(p.apply_async(f, [i]))
resPool.append(p.apply_async(rand))
print('All Processes launched')
res = []
for r in resPool:
res.append(r.get())
t2= time.time()
print('Time: ', t2 - t1)
p.close()
return res