python进程之间共享数据
-
Value
# Value是multiprocessing库提供的对象类
# 示例:
from multiprocessing import Process, Value
def task(num: Value):
# 提供锁解决同步问题
with num.get_lock():
num.value += 1
print(f'process_num={num.value}')
if __name__ == '__main__':
num = Value('i', 1) # 'i'表示整型变量
process_task = Process(target=task, args=(num,))
process_task.start()
process_task.join()
print(f'global_num={num.value}')
-
# 使用方式与Value一样,只不过是传递共享的列表
# 示例:
from multiprocessing import Process, Array
def task(arr: Array):
with arr.get_lock():
for i in range(len(arr)):
arr[i] = pow(arr[i], 2)
if __name__ == '__main__':
arr = Array('i', range(10)) # 定义共享内存变量,i表示整数
process_task = Process(target=task, args=(arr,))
process_task.start()
process_task.join()
print(arr[:])
-
sharedctypes
# 除了以上Value和Array,multiprocessing还提供了sharedctypes来实现共享内存,sharedctypes内部包含多种共享内存的创建类
multiprocessing.sharedctypes.RawValue
multiprocessing.sharedctypes.copy
multiprocessing.sharedctypes.RawArray
multiprocessing.sharedctypes.synchronized
multiprocessing.sharedctypes.Value
multiprocessing.sharedctypes.Array -
服务器进程
# 除了使用共享内存, 进程之间通信还可以使用服务器进程(Server process)
# 示例
from multiprocessing import Process, Manager
def task(share_dict, share_list, lock):
with lock:
for i in range(5):
share_dict[f'key-{i}'] = f'value-{i}'
share_list.append(f'element-{i}')
if __name__ == '__main__':
with Manager() as manager:
share_dict = manager.dict() # 通过manager创建字典
share_list = manager.list() # 通过manager创建列表
lock = manager.Lock() # # 通过manager创建锁
process_task = Process(target=task, args=(share_dict, share_list, lock))
process_task.start()
process_task.join()
print(share_dict)
print(share_list)
# ps:这种方式不仅可以在同一机器的不同进程之间共享数据,还可以在不同机器之间共享数据.