当前位置:网站首页>多进程全局变量失效、变量共享问题
多进程全局变量失效、变量共享问题
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)
边栏推荐
猜你喜欢

小实战项目之——吃货联盟订餐系统

2.(1)栈的链式存储、链栈的操作(图解、注释、代码)

接口报错no message avaliable

Conditional statements of shell (test, if, case)

FTP服务与配置

【云原生】-Docker容器迁移Oracle到MySQL

Zero-Shot Learning & Domain-aware Visual Bias Eliminating for Generalized Zero-Shot Learning

安装和使用uView

Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)

深度解析 z-index
随机推荐
Zotero | Zotero translator插件更新 | 解决百度学术文献无法获取问题
Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)
深度解析 z-index
Difficulty comparison between high concurrency and multithreading (easy to confuse)
数据库原理作业3 — JMU
剑指offer(一)
如何在uni-app中选择一个合适的UI组件库
leetcode 406. Queue Reconstruction by Height
frp内网穿透服务
浅析v-model语法糖的实现原理与细节知识及如何让你开发的组件支持v-model
Kubernetes scheduling
shell脚本 -d 是目录文件,那么-e,-f等说明
进程和计划任务管理
QFileInfo常规方法
postgresql源码学习(33)—— 事务日志⑨ - 从insert记录看日志写入整体流程
Detailed explanation of js prototype
Install the gstreamer development dependency library to the project sysroot directory
什么是浮动?什么是文档流?清除浮动的几种方式及原理?什么是BFC,如何触发BFC,BFC的作用
cp 的用法
LeetCode刷题——摆动序列#376#Medium