Python 3.x 如何在python3中从processpool executer捕获异常
如何从processpool执行器捕获异常?我使用了result(),但它会减慢进程,我想还有其他方法吗Python 3.x 如何在python3中从processpool executer捕获异常,python-3.x,parallel-processing,Python 3.x,Parallel Processing,如何从processpool执行器捕获异常?我使用了result(),但它会减慢进程,我想还有其他方法吗 with concurrent.futures.ProcessPoolExecutor(50) as genrator: try: for fl in self.finallist: print("FIKLENAME",fl)
with concurrent.futures.ProcessPoolExecutor(50) as genrator:
try:
for fl in self.finallist:
print("FIKLENAME",fl)
futures=[genrator.submit(insta.load_instantel_ascii, fl,None,',')]
results = [f.result() for f in futures]
except Exception as e:
print("EXCE", e)
print("FILENAME IS",fl)
使用
concurrent.futures.as_completed
获取迭代器,该迭代器在期货完成时生成期货。如果您使用此API,这里的收益应该是最小的
还要构建一个可在for循环之外使用的未来列表
编辑
要获取异常块内部的fl
,例如print('exception:',exc.fl)
,您可以捕获调用方中的异常并使用绑定到fl
的名称重新引发它,或者使用绑定到调用方中引发的fl
的属性实现自定义异常
对于前者,如果您没有修改权限,请修改insta.load\u instantel\u ascii
def try_except_fl(method, *args, **kwargs):
try:
return method(*args, **kwargs)
except Exception as exc:
fl = args[0]
exc.fl = fl
raise exc
如果您有权修改insta.load\u instantel\u ascii
,请将该过程包装在try/except
中
def load_instantel_ascii(self, fl, *args, **kwargs):
try:
# do some compute intensive tasks here
except Exception as exc:
exc.fl = fl
raise exc
对于后者,使用绑定到fl
的属性引发自定义异常。e、 g
在失败点提出这一点,即<代码>加载\u实例电话\u ascii
def load_instantel_ascii(self, fl, *args, **kwargs):
try:
# do some compute intensive tasks here
except Exception as exc:
raise ProcessFailure(fl)
让我试一试如何将fl变量放入exeption块?您可以引发自定义异常,并将其作为自定义异常的已引发实例的属性传递。好的,让我试一试try@chris1234参见编辑
class ProcessFailure(Exception): # you may want to subclass a specialized exception class.
message = 'Alas! This task had a lot of errors.'
def __init__(fl):
self.fl = fl
def load_instantel_ascii(self, fl, *args, **kwargs):
try:
# do some compute intensive tasks here
except Exception as exc:
raise ProcessFailure(fl)