【解决方案】调用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             return
    784 
--> 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(…)

强制类型转换即可。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肥宅_Sean

公众号“肥宅Sean”欢迎关注

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值