多进程报错
多进程,获取结果 .get() 报错:
AttributeError: Can't pickle local object 'EdgeSliceProcess.get_points..row_con_min'
python闭包不支持pickle(序列化)。多进程需要函数能pickle。
有几种方式解决:
0.将函数体中的函数挪出。
这是最简单的方法。但是有时因为函数接口已经给定,需要使用函数来定义函数。考虑以下两种方法。
1.更换pickle,考虑以下代码:
from pathos.multiprocessing import ProcessingPool as Pool
网址
https://github.com/uqfoundation/pathos
2.避免使用闭包
多进程函数不要有闭包结构,这个有用的!!!
3.改用队列 multiprocessing Queue 来通信
注意:创建进程慢:12个进程,创建大概 5 秒
创建多进程:报错:
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
if name == 'main':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
解决:
1 在 __main__ 下创建进程
2 创建进程早了,要在主进程创建后,创建子进程,比如,在函数调用的时候创建
主进程:
pool = [] # 单例模式
def aaa():
if pool == []:
创建子进程池
注意: 固定的进程数量,还是,变动的进程数量
创建固定始终运行进程和使用进程池
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现