问题
在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(…) |
强制类型转换即可。