当前位置:网站首页>多进程全局变量失效、变量共享问题
多进程全局变量失效、变量共享问题
2022-07-31 05:47:00 【loong_XL】
*多进程因为不共享空间,函数内部append,±等处理函数外部变量会不起作用
参考:https://www.bilibili.com/video/BV1EZ4y1X7Aj
https://www.bilibili.com/video/BV1p64y1i7KA
1、共享可以通过间接return返回值保存pp,迭代再处理
#多进程
from multiprocessing import Pool
def foo(x):
return x*x
if __name__=="__main__":
p = Pool(processes=5)
pp = p.imap(foo,range(10))
print(pp)
kk=[]
for ii in tqdm(pp):
kk.append(ii)
print(kk)
2、可以用共享内存Manager
from multiprocessing import Pool,Value,Manager,Lock
# import time
#
from tqdm import tqdm
#
#
def test(i,lists,lock):
with lock:
print(i)
lists.append(i**3)
if __name__=="__main__":
print(111)
with Manager() as manager:
share_list = manager.list()
lock = manager.Lock()
pool = Pool(processes=2)
for i in tqdm(range(100)):
pool.apply(test,args=(i,share_list,lock))
print(share_list)
边栏推荐
- mysql的下载及安装使用
- 批量翻译软件免费【2022最新版】
- 批量免费文字翻译
- 讲解实例+详细介绍@Resource与@Autowired注解的区别(全网最全)
- Install the gstreamer development dependency library to the project sysroot directory
- (border-box) The difference between box model w3c and IE
- 浅层了解欧拉函数
- gstreamer的caps event和new_segment event
- 试题 历届真题 错误票据【第四届】【省赛】【B组】
- Analysis of the principle and implementation of waterfall flow layout
猜你喜欢
随机推荐
Zotero | Zotero translator plugin update | Solve the problem that Baidu academic literature cannot be obtained
js原型详解
Koa框架的基本使用
Analysis of pseudo-classes and pseudo-elements
360推送-360推送工具-360批量推送工具
DNS域名解析服务
自动翻译软件-批量批量自动翻译软件推荐
批量翻译软件免费【2022最新版】
剑指offer(一)
批量免费文字翻译
DHCP原理与配置
shell之条件语句(test、if、case)
tidyverse笔记——dplyr包
tidyverse笔记——tidyr包
FTP服务与配置
单点登录 思维导图
LVM和磁盘配额
Exam Questions Previous True Questions Wrong Bills [The Fourth Session] [Provincial Competition] [Group B]
【Star项目】小帽飞机大战(七)
nohup principle









