-
Notifications
You must be signed in to change notification settings - Fork 0
/
testParallel.py
58 lines (44 loc) · 1.1 KB
/
testParallel.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
49
50
51
52
53
54
55
56
import multiprocessing as mp
import numpy as np
sharedArray = mp.Array('i',10)
sharedValue = mp.Value('i')
def incrA(a,i):
for j in range(10):
a[j]+=i
def incrV(v,i):
with v.get_lock():
v.value+=i
def incrVs(v,i):
with v[0].get_lock():
v[0].value+=i
with v[1].get_lock():
v[1].value+=2*i
process = []
for i in range(10):
process.append(mp.Process(target=incrA,args=(sharedArray,i)))
process[i].start()
for i in range(10):
process[i].join()
print sharedArray[0], sharedArray[1], sharedArray[2], sharedArray[3]
process = []
for i in range(1000):
process.append(mp.Process(target=incrV,args=(sharedValue,i)))
process[i].start()
for i in range(1000):
process[i].join()
print sharedValue.value
v=0
for i in range(1000):
v+=i
print int(0.5*999*1000)
sharedValue0 = mp.Value('i')
sharedValue1 = mp.Value('i')
sharedValues = [sharedValue0,sharedValue1]
process = []
for i in range(1000):
process.append(mp.Process(target=incrVs,args=(sharedValues,i)))
process[i].start()
for i in range(1000):
process[i].join()
print sharedValues[0].value
print sharedValues[1].value