欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

【解决方案】调用multiprocessing中创建的文件无法打开的问题FileNotFoundError: [WinError 2]

发布时间:2025/4/16 46 豆豆
生活随笔 收集整理的这篇文章主要介绍了 【解决方案】调用multiprocessing中创建的文件无法打开的问题FileNotFoundError: [WinError 2] 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

问题

在python多进程任务中,常常会使用共有的变量,交给Manager管理。

但是这样写入的到的文件,有时候会由于我们操作的遗漏。导致没办法在其他文件中打开对应的压缩文件。

比如报下面这样的错误:

FileNotFoundError Traceback (most recent call last) <ipython-input-48-d03f6b842aa5> in <module>3 for filename in filenamelist:4 with open(os.path.join('./pickle', filename), 'rb') as f: ----> 5 word_list = pickle.load(f)c:\users\lijy2\appdata\local\programs\python\python36\lib\multiprocessing\managers.py in RebuildProxy(func, token, serializer, kwds)879 not getattr(process.current_process(), '_inheriting', False)880 ) --> 881 return func(token, serializer, incref=incref, **kwds)882 883 #c:\users\lijy2\appdata\local\programs\python\python36\lib\multiprocessing\managers.py in __init__(self, token, serializer, manager, authkey, exposed, incref, manager_owned)729 730 if incref: --> 731 self._incref()732 733 util.register_after_fork(self, BaseProxy._after_fork)c:\users\lijy2\appdata\local\programs\python\python36\lib\multiprocessing\managers.py in _incref(self)783 return784 --> 785 conn = self._Client(self._token.address, authkey=self._authkey)786 dispatch(conn, None, 'incref', (self._id,))787 util.debug('INCREF %r', self._token.id)c:\users\lijy2\appdata\local\programs\python\python36\lib\multiprocessing\connection.py in Client(address, family, authkey)483 _validate_family(family)484 if family == 'AF_PIPE': --> 485 c = PipeClient(address)486 else:487 c = SocketClient(address)c:\users\lijy2\appdata\local\programs\python\python36\lib\multiprocessing\connection.py in PipeClient(address)684 while 1:685 try: --> 686 _winapi.WaitNamedPipe(address, 1000)687 h = _winapi.CreateFile(688 address, _winapi.GENERIC_READ | _winapi.GENERIC_WRITE,FileNotFoundError: [WinError 2]

解决办法

实际上,是我们在pickle打包前。
保存的文件是Manager的对应元素
需要进行的操作很简单,就是将其转成对应的Python元素之后,再打包就好了。

比如:

原来操作
multiprocessing.Manager().list()list(…)
multiprocessing.Manager().dict()dict(…)

强制类型转换即可。

总结

以上是生活随笔为你收集整理的【解决方案】调用multiprocessing中创建的文件无法打开的问题FileNotFoundError: [WinError 2]的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。