猿问

AttributeError:“str”对象没有属性“errno”

我ClientConnectionError在multiprocessing.Queue由asyncio. 我这样做是为了将在 asyncio 中生成的异常传递回另一个线程/进程中的客户端。


我的假设是这个异常发生在反序列化过程中,从队列中读取异常。否则看起来几乎不可能达到。


Traceback (most recent call last):

  File "model_neural_simplified.py", line 318, in <module>

    main(**arg_parser())

  File "model_neural_simplified.py", line 314, in main

    globals()[command](**kwargs)

  File "model_neural_simplified.py", line 304, in predict

    next_neural_data, next_sample = reader.get_next_result()

  File "/project_neural_mouse/src/asyncs3/s3reader.py", line 174, in get_next_result

    result = future.result()

  File "/usr/lib/python3.6/concurrent/futures/_base.py", line 432, in result

    return self.__get_result()

  File "/usr/lib/python3.6/concurrent/futures/_base.py", line 384, in __get_result

    raise self._exception

  File "/usr/lib/python3.6/concurrent/futures/thread.py", line 56, in run

    result = self.fn(*self.args, **self.kwargs)

  File "model_neural_simplified.py", line 245, in read_sample

    f_bytes = s3f.read(read_size)

  File "/project_neural_mouse/src/asyncs3/s3reader.py", line 374, in read

    size, b = self._issue_request(S3Reader.READ, (self.url, size, self.position))

  File "/project_neural_mouse/src/asyncs3/s3reader.py", line 389, in _issue_request

    response = self.communication_channels[uuid].get()

  File "/usr/lib/python3.6/multiprocessing/queues.py", line 113, in get

    return _ForkingPickler.loads(res)

  File "/usr/local/lib/python3.6/dist-packages/aiohttp/client_exceptions.py", line 133, in __init__

    super().__init__(os_error.errno, os_error.strerror)

AttributeError: 'str' object has no attribute 'errno'

我认为这是一个很长的问题,但是有人知道这个问题吗?


Python 3.6.8,aiohttp.__version__ == 3.6.0


哆啦的时光机
浏览 263回答 2
2回答

慕森王

OSError有一个自定义__reduce__实现;不幸的是,对于与预期参数不匹配的子类,它不是子类友好的。__reduce__手动调用可以看到酸洗的中间状态:>>> SubOSError.__reduce__(cce)(modulename.SubOSError, (1, 'unittest'))的第一个元素tuple是可调用的,第二个是tuple要传递的参数。因此,当它尝试重新创建您的课程时,它会:modulename.SubOSError(1, 'unittest')丢失了有关OSError您最初创建时使用的信息。OSError.__reduce__如果您必须接受与/预期不匹配的参数OSError.__init__,您将需要编写自己的__reduce__覆盖以确保正确的信息被腌制。一个简单的版本可能是:class SubOSError(OSError):&nbsp; &nbsp; def __init__(self, foo, os_error):&nbsp; &nbsp; &nbsp; &nbsp; self.foo = foo&nbsp; # Must preserve information for pickling later&nbsp; &nbsp; &nbsp; &nbsp; super().__init__(os_error.errno, os_error.strerror)&nbsp; &nbsp; def __reduce__(self):&nbsp; &nbsp; &nbsp; &nbsp; # Pickle as type plus tuple of args expected by type&nbsp; &nbsp; &nbsp; &nbsp; return type(self), (self.foo, OSError(*self.args))使用该设计,SubOSError.__reduce__(cce)现在将返回:(modulename.SubOSError, (1, PermissionError(1, 'unittest')))其中 的第二个元素tuple是重新创建实例所需的正确参数(从OSError到的变化PermissionError是预期的;OSError实际上返回基于 的自己的子类errno)。

LEATH

此问题已于 2019 年 9 月 25 日修复并合并到 master 中aiohttp。如果我注意到修复程序进入的版本,我将在未来更新此答案(请随时编辑此答案以记录包含此更新的版本)&nbsp;.修复的 Git 问题:https://github.com/aio-libs/aiohttp/issues/4077
随时随地看视频慕课网APP

相关分类

Python
我要回答