【解决方案】调用multiprocessing中创建的文件无法打开的问题FileNotFoundError: [WinError 2]
生活随笔
收集整理的這篇文章主要介紹了
【解决方案】调用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]的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: FCM算法实现Python(简洁版)
- 下一篇: VSCode输出框中文乱码问题