# 这是代码,直接,可以运行
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.
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.