IPC in Python(multiprocessing)
最近在工作中使用IPC(Inter-Process Communication), 发现直接使用全局变量在多个进程间通信是不可行的,代码如下: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# 0. 使用全局变量在多个进程间通信(不可行)
global_val = [27017]
def func0(value):
global global_val
global_val.append(value)
print("[func0()]pid:", os.getpid(), ", global_val:", global_val, ", id(global_val):", id(global_val))
def global_var_IPC():
"""
直接使用全局变量在多个进程间通信是不可行的
"""
global global_val
print("[main]pid:", os.getpid(), ", global_val:", global_val, ", id(global_val):", id(global_val))
processes = []
for i in range(5):
process = multiprocessing.Process(target=func0, args=(i, ))
process.start()
processes.append(process)
for process in processes:
process.join()
print("[main]pid:", os.getpid(), ", global_val:", global_val, ", id(global_val):", id(global_val))
输出结果如下:1
2
3
4
5
6
7[main]pid: 2319 , global_val: [27017] , id(global_val): 140655214689160
[func()]pid: 2320 , global_val: [27017, 0] , id(global_val): 140655214689160
[func()]pid: 2321 , global_val: [27017, 1] , id(global_val): 140655214689160
[func()]pid: 2322 , global_val: [27017, 2] , id(global_val): 140655214689160
[func()]pid: 2323 , global_val: [27017, 3] , id(global_val): 140655214689160
[func()]pid: 2324 , global_val: [27017, 4] , id(global_val): 140655214689160
[main]pid: 2319 , global_val: [27017] , id(global_val): 140655214689160
1. 进程间共享数值型数据
1 | # 1. 进程间共享数值型数据 |
输出结果如下:1
2
3
4
5
6
7
8
9[main]pid: 2880 , multi_v.value: 10.0 , id(multi_v.value): 139652794868672
[func2]pid: 2881 , obj.value: 10.0 , id(obj.value): 139652794869008
[func2]pid: 2881 , obj.value: 10.78 , id(obj.value): 139652794869008
[main]pid: 2880 , multi_v.value: 10.78 , id(multi_v.value): 139652794869008
------------------------------------------------------------
[main]pid: 2880 , myobj.value: 10 , id(myobj.value): 9469696
[func2]pid: 2882 , obj.value: 10 , id(obj.value): 9469696
[func2]pid: 2882 , obj.value: 10.78 , id(obj.value): 139652794867832
[main]pid: 2880 , myobj.value: 10 , id(myobj.value): 9469696
2. 进程间共享数组型数据
1 | # 2. 进程间共享数组型数据 |
输出结果如下:1
2
3
4[main]pid: 8965 , array: [1, 2, 3, 4, 5, 6] , id(array): 140127608142648
[func2]pid: 8966 , array: [1, 2, 3, 4, 5, 6] , id(array): 140127608142648
[func2]pid: 8966 , array: [1, 2, -110, 4, 5, 6] , id(array): 140127608142648
[main]pid: 8965 , array: [1, 2, -110, 4, 5, 6] , id(array): 140127608142648
3. 进程间共享dict,list数据
1 | # 3. 进程间共享dict,list数据 |
输出结果如下:1
2
3
4
5
6
7
8[main]pid: 14180 , mydic: {} , id(mydic): 140480908666808
[main]pid: 14180 , mylist: [0, 1, 2, 3, 4] , id(mylist): 140480908701768
[func3]pid: 14196 , mydic: {} , id(mydic): 140480908666808
[func3]pid: 14196 , mylist: [0, 1, 2, 3, 4] , id(mylist): 140480908701768
[func3]pid: 14196 , mydic: {'index1': 'value1', 'index2': 'value2'} , id(mydic): 140480908666808
[func3]pid: 14196 , mylist: [0, 1, 2, 3, 4, 111, 222, 333] , id(mylist): 140480908701768
[main]pid: 14180 , mydic: {'index1': 'value1', 'index2': 'value2'} , id(mydic): 140480908666808
[main]pid: 14180 , mylist: [0, 1, 2, 3, 4, 111, 222, 333] , id(mylist): 140480908701768
多线程间的通信可以使用全局变量(global), 并在必要时使用锁.