#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
"""
@File : 共享变量.py
@Time : 2022-09-03 10:51
@Author : 坐公交也用券
@Version : 1.0
@Contact : faith01238@hotmail.com
@Homepage : https://liumou.site
@Desc : 当前文件作用
"""
from multiprocessing import Process, Manager
def s(d, l , lk):
with lk:
for i in range(5):
d[f"dic_{i}"] = i
l.append(f"list_{i}")
if __name__ == '__main__':
with Manager() as man:
share_dic = man.dict()
share_list = man.list()
lock = man.Lock()
p = Process(target=s, args=(share_dic, share_list, lock))
p.start()
p.join()
print(share_dic)
print(share_list)
运行结果
/usr/bin/python3.7 /media/liumou/exfat/code/y/Python_Demo/多进程/共享变量.py
{'dic_0': 0, 'dic_1': 1, 'dic_2': 2, 'dic_3': 3, 'dic_4': 4}
['list_0', 'list_1', 'list_2', 'list_3', 'list_4']