-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_mp.py
More file actions
55 lines (50 loc) · 1.05 KB
/
python_mp.py
File metadata and controls
55 lines (50 loc) · 1.05 KB
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
import multiprocessing
from multiprocessing import Pool
from multiprocessing import Process
def f(x, rslt):
rslt.append(x*x)
print(rslt)
return x*x
def test_process_vars():
rslt=[]
ps=[]
for _ in range(3):
p = Process(target=f, args=(3, rslt))
p.start()
ps.append(p)
for p in ps:
p.join()
print("----")
print(rslt) # []
def test_process_return():
rslt=[]
ps=[]
for _ in range(3):
p = Process(target=f, args=(3, rslt))
p.start()
ps.append(p)
for p in ps:
rslt.append(p.join())
print("----")
print(rslt) # [None, None, None]
def test_process_shared_mem():
manager = multiprocessing.Manager()
rslt=manager.list()
ps=[]
for i in range(3):
p = Process(target=f, args=(i, rslt))
p.start()
ps.append(p)
for p in ps:
p.join()
print("----")
print(rslt)
"""
[0]
[0, 1]
[0, 1, 4]
----
[0, 1, 4]
"""
if __name__ == '__main__':
test_process_shared_mem()