今天封楼被困公司,研究了一下 ProcessPoolExecutor,发现了一个 Python 的 Bug,分享给各位,网上似乎没解决方案

2022-04-25 22:30:36 +08:00
 0x0208v0

用的 python3.7.13 。报错信息如下兄弟们:

很奇怪为什么不能 raise key-value 格式的自定义错误 ,

要是改成 raise MyError( 1, 'desc') 就没问题了,这是不是 python 的 bug?

# 这是代码,直接,可以运行
from concurrent.futures.process import ProcessPoolExecutor


class MyError(Exception):
    def __init__(self, code: int, desc: str):
        self.code = code
        self.desc = desc


def foo(i, j):
    print(i, j)
    raise MyError(code=1, desc='desc')


def main():
    with ProcessPoolExecutor(3) as executor:
        f = executor.submit(foo, 1, 11)
        e = f.exception()
        print(e)

        f.result()


if __name__ == '__main__':
    main()

下面是输出结果:

1 11
A process in the process pool was terminated abruptly while the future was running or pending.
concurrent.futures.process._RemoteTraceback: 
'''
Traceback (most recent call last):
  File "/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/process.py", line 367, in _queue_management_worker
    result_item = result_reader.recv()
  File "/Python.framework/Versions/3.7/lib/python3.7/multiprocessing/connection.py", line 251, in recv
    return _ForkingPickler.loads(buf.getbuffer())
TypeError: __init__() missing 2 required positional arguments: 'code' and 'desc'
'''

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/v2exblog/demo/aaa.py", line 25, in <module>
    main()
  File "/Users/v2exblog/demo/aaa.py", line 21, in main
    f.result()
  File "/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/_base.py", line 428, in result
    return self.__get_result()
  File "/Python.framework/Versions/3.7/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
    raise self._exception
concurrent.futures.process.BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.
3761 次点击
所在节点    Python
3 条回复
thinkershare
2022-04-25 22:45:28 +08:00
封送反序列化的问题, 问题出在 raise 这里, 使用命名参数构造的对象无法按照预期方式反序列化, 错误对象都是构建成功了的.
thinkershare
2022-04-25 22:47:09 +08:00
新版本的 python 没啥问题, 已经修复了
julyclyde
2022-04-26 11:04:12 +08:00
required positional arguments

它认为__init__里边那俩是位置参数?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/849250

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX