MAC电脑运行python并发编程遇到的问题
在python多进程编程过程中出现如下问题:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | from multiprocessing import Process,Queue,set_start_method,get_context def download_from_web(q): data = [ 11 , 22 , 33 , 44 ] for temp in data: q.put(temp) def analysis_data(q): """处理数据""" watting_analysis_data = list () while True : data = q.get() watting_analysis_data.append(data) if q.empty(): break print (watting_analysis_data) def main(): q = Queue() p1 = Process(target = download_from_web,args = (q,)) p2 = Process(target = analysis_data,args = (q,)) p1.start() p2.start() if __name__ = = "__main__" : main() |
运行会报错:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | Traceback (most recent call last): File "<string>" , line 1 , in <module> File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py" , line 116 , in spawn_main exitcode = _main(fd, parent_sentinel) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py" , line 126 , in _main self = reduction.pickle.load(from_parent) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/synchronize.py" , line 110 , in __setstate__ self ._semlock = _multiprocessing.SemLock._rebuild( * state) FileNotFoundError: [Errno 2 ] No such file or directory Traceback (most recent call last): File "<string>" , line 1 , in <module> File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py" , line 116 , in spawn_main exitcode = _main(fd, parent_sentinel) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/spawn.py" , line 126 , in _main self = reduction.pickle.load(from_parent) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/synchronize.py" , line 110 , in __setstate__ self ._semlock = _multiprocessing.SemLock._rebuild( * state) FileNotFoundError: [Errno 2 ] No such file or directory |
实际上,Python 创建的子进程执行的内容,和启动该进程的方式有关。而根据不同的平台,启动进程的方式大致可分为以下 3 种:
- spawn:使用此方式启动的进程,只会执行和 target 参数或者 run() 方法相关的代码。Windows 平台只能使用此方法,事实上该平台默认使用的也是该启动方式。相比其他两种方式,此方式启动进程的效率最低。
- fork:使用此方式启动的进程,基本等同于主进程(即主进程拥有的资源,该子进程全都有)。因此,该子进程会从创建位置起,和主进程一样执行程序中的代码。注意,此启动方式仅适用于 UNIX 平台,os.fork() 创建的进程就是采用此方式启动的。
- forserver:使用此方式,程序将会启动一个服务器进程。即当程序每次请求启动新进程时,父进程都会连接到该服务器进程,请求由服务器进程来创建新进程。通过这种方式启动的进程不需要从父进程继承资源。注意,此启动方式只在 UNIX 平台上有效。
原因是MAC电脑默认启动进程的方式是fork,而python默认的方式是spawn,所以需要将python启动进程的方式做修改:
from multiprocessing import Process,Queue,set_start_method,get_context def download_from_web(q): data = [11,22,33,44] for temp in data: q.put(temp) def analysis_data(q): """处理数据""" watting_analysis_data = list() while True: data = q.get() watting_analysis_data.append(data) if q.empty(): break print(watting_analysis_data) def main():
set_start_method('fork') q = Queue() p1 = Process(target=download_from_web,args=(q,)) p2 = Process(target=analysis_data,args=(q,)) p1.start() p2.start() if __name__ == "__main__": main()
也可以使用 get_context() 来获取上下文对象。上下文对象与多处理模块具有相同的API,并允许在同一程序中使用多个启动方法,如下:
from multiprocessing import Process,Queue,set_start_method,get_context def download_from_web(q): data = [11,22,33,44] for temp in data: q.put(temp) def analysis_data(q): """处理数据""" watting_analysis_data = list() while True: data = q.get() watting_analysis_data.append(data) if q.empty(): break print(watting_analysis_data) def main(): q = Queue() ctx = get_context('fork') p1 = ctx.Process(target=download_from_web,args=(q,)) p2 = ctx.Process(target=analysis_data,args=(q,)) p1.start() p2.start() if __name__ == "__main__": main()
这样就成功获取到我们的结果了:
[11, 22, 33, 44]
-------------------------------------------------------------------------------------------------------------------------------------------
另外如下代码也可以正常得到结果:
import multiprocess def download_from_web(q): data = [11,22,33,44] for temp in data: q.put(temp) def analysis_data(q): """处理数据""" watting_analysis_data = list() while True: data = q.get() watting_analysis_data.append(data) if q.empty(): break print(watting_analysis_data) def main(): q = multiprocess.Queue() p1 = multiprocess.Process(target=download_from_web,args=(q,)) p2 = multiprocess.Process(target=analysis_data,args=(q,)) p1.start() p2.start() if __name__ == "__main__": main()
posted on 2021-04-26 22:53 torotoise512 阅读(1826) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
2019-04-26 二维数组赋值
2019-04-26 【转载】Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码