Skip to content

Commit cf47935

Browse files
committed
fix
1 parent 98c4362 commit cf47935

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Python 多进程之间共享变量
2+
3+
Python 多线程之间共享变量很简单,直接定义全局 global 变量即可。而多进程之间是相互独立的执行单元,这种方法就不可行了。
4+
5+
不过 Python 标准库已经给我们提供了这样的能力,使用起来也很简单。但要分两种情况来看,一种是 Process 多进程,一种是 Pool 进程池的方式。
6+
7+
### Process 多进程
8+
9+
使用 Process 定义的多进程之间共享变量可以直接使用 multiprocessing 下的 Value,Array,Queue 等,如果要共享 list,dict,可以使用强大的 Manager 模块。
10+
11+
```python
12+
import multiprocessing
13+
14+
15+
def func(num):
16+
# 共享数值型变量
17+
# num.value = 2
18+
19+
# 共享数组型变量
20+
num[2] = 9999
21+
22+
23+
if __name__ == '__main__':
24+
# 共享数值型变量
25+
# num = multiprocessing.Value('d', 1)
26+
# print(num.value)
27+
28+
# 共享数组型变量
29+
num = multiprocessing.Array('i', [1, 2, 3, 4, 5])
30+
print(num[:])
31+
32+
p = multiprocessing.Process(target=func, args=(num,))
33+
p.start()
34+
p.join()
35+
36+
# 共享数值型变量
37+
# print(num.value)
38+
39+
# 共享数组型变量
40+
print(num[:])
41+
```
42+
43+
### Pool 进程池
44+
45+
进程池之间共享变量是不能使用上文方式的,因为进程池内进程关系并非父子进程,想要共享,必须使用 Manage 模块来定义。
46+
47+
```python
48+
from multiprocessing import Pool, Manager
49+
50+
51+
def func(my_list, my_dict):
52+
my_list.append(10)
53+
my_list.append(11)
54+
my_dict['a'] = 1
55+
my_dict['b'] = 2
56+
57+
58+
if __name__ == '__main__':
59+
manager = Manager()
60+
my_list = manager.list()
61+
my_dict = manager.dict()
62+
63+
pool = Pool(processes=2)
64+
for i in range(0, 2):
65+
pool.apply_async(func, (my_list, my_dict))
66+
pool.close()
67+
pool.join()
68+
69+
print(my_list)
70+
print(my_dict)
71+
```
72+
73+
还有一点需要注意,在共享 list 时,像下面这样写 func 是不起作用的。
74+
75+
```python
76+
def func(my_list, my_dict):
77+
my_list = [10, 11]
78+
my_dict['a'] = 1
79+
my_dict['b'] = 2
80+
```
81+
82+
这样写相当于重新定义了一个局部变量,并没有作用到原来的 list 上,必须使用 append,extend 等方法。
83+
84+
**参考文档:**
85+
86+
https://blog.csdn.net/houyanhua1/article/details/78244288
87+
88+
**往期精彩:**

python/src/multi_process.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import multiprocessing
2+
3+
4+
def func(num):
5+
# 共享数值型变量
6+
# num.value = 10.78
7+
8+
# 共享数组型变量
9+
num[2] = 9999
10+
11+
12+
if __name__ == '__main__':
13+
# 共享数值型变量
14+
# num = multiprocessing.Value('d', 10.0)
15+
# print(num.value)
16+
17+
# 共享数组型变量
18+
num = multiprocessing.Array('i', [1, 2, 3, 4, 5])
19+
print(num[:])
20+
21+
p = multiprocessing.Process(target=func, args=(num,))
22+
p.start()
23+
p.join()
24+
25+
# 共享数值型变量
26+
# print(num.value)
27+
28+
# 共享数组型变量
29+
print(num[:])

python/src/multi_process_pool.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from multiprocessing import Pool, Manager
2+
3+
4+
def func(my_list, my_dict):
5+
my_list.append(10)
6+
my_list.append(11)
7+
my_dict['a'] = 1
8+
my_dict['b'] = 2
9+
10+
11+
if __name__ == '__main__':
12+
manager = Manager()
13+
my_list = manager.list()
14+
my_dict = manager.dict()
15+
16+
pool = Pool(processes=2)
17+
for i in range(0, 2):
18+
pool.apply_async(func, (my_list, my_dict))
19+
pool.close()
20+
pool.join()
21+
22+
print(my_list)
23+
print(my_dict)

0 commit comments

Comments
 (0)