0

今天封楼被困公司,研究了一下 ProcessPoolExecutor,发现了一个 Python 的 Bug,分享...

 2 years ago
source link: https://www.v2ex.com/t/849250
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Python

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

  v2exblog · 22 小时 56 分钟前 · 1396 次点击

用的 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.

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK