# 这是代码,直接,可以运行
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.
1
thinkershare 2022-04-25 22:45:28 +08:00
封送反序列化的问题, 问题出在 raise 这里, 使用命名参数构造的对象无法按照预期方式反序列化, 错误对象都是构建成功了的.
|
2
thinkershare 2022-04-25 22:47:09 +08:00
新版本的 python 没啥问题, 已经修复了
|
3
julyclyde 2022-04-26 11:04:12 +08:00
required positional arguments
它认为__init__里边那俩是位置参数? |