python3如何在应用error\u回调时找出apply\u异步目标函数中的哪一行导致了错误
使用python 3.4 我有一个失败的目标函数,并应用_async调用我的错误回调函数而不是回调函数 问题是捕获并发送到error_回调的异常没有帮助,我无法确定目标函数中的错误发生在哪里 我的错误回调python3如何在应用error\u回调时找出apply\u异步目标函数中的哪一行导致了错误,python,python-3.x,multiprocessing,python-multiprocessing,Python,Python 3.x,Multiprocessing,Python Multiprocessing,使用python 3.4 我有一个失败的目标函数,并应用_async调用我的错误回调函数而不是回调函数 问题是捕获并发送到error_回调的异常没有帮助,我无法确定目标函数中的错误发生在哪里 我的错误回调 def error_callback(self,e): print('error_callback()',e) # tested one of these at a time traceback.print_exc() traceback.print_las
def error_callback(self,e):
print('error_callback()',e)
# tested one of these at a time
traceback.print_exc()
traceback.print_last()
traceback.print_stack()
以及回溯输出
打印exc
File "/usr/lib/python3.4/traceback.py", line 125, in _iter_chain
context = exc.__context__
AttributeError: 'NoneType' object has no attribute '__context__'
最后打印
File "/usr/lib/python3.4/traceback.py", line 262, in print_last
raise ValueError("no last exception")
ValueError: no last exception
打印堆栈
File "./relative/path/to/my/script.py", line 71, in traceback
traceback.print_stack()
我不得不承认,我对回溯库以及如何使用它感到不舒服,但在我看来,在这种情况下不可能使用回溯
我该怎么办?您正在调用的
回溯
函数都希望从sys.exec\u info
获取异常信息,在捕获异常时,异常信息通常存储在该位置。但是,由于您案例中的实际异常来自子进程,因此当您在父进程中调用它们时,它们找不到任何异常信息
您需要使用从子进程接收的异常对象e
提取相关信息。尝试使用回溯。使用适当的参数打印异常
:
traceback.print_exception(type(e), e, e.__traceback__)
我不知道目标函数中的错误发生在哪里
下面是一个您可以做的示例:
import multiprocessing as mp
def do_stuff(num):
if False:
x = 10
else:
x = 20
result = 5/(2 - num) #Line 9
return result
def success_handler(result):
print('success')
def error_handler(e):
print('error')
print(dir(e), "\n")
print("-->{}<--".format(e.__cause__))
with mp.Pool(2) as my_pool:
results = [
my_pool.apply_async(
do_stuff,
args=(i,),
callback=success_handler,
error_callback=error_handler
)
for i in range(3)
]
try:
for result_obj in results:
print("result is: {}".format(result_obj.get()))
except ZeroDivisionError as e:
print("Oh, boy. This is the second time I've seen this error.")
--output:--
success
success
result is: 2.5
result is: 5.0
error
['__cause__', '__class__', '__context__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__suppress_context__', '__traceback__', 'args', 'with_traceback']
-->
"""
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/multiprocessing/pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "1.py", line 10, in do_stuff
result = 5/(2 - num) #Line 9
ZeroDivisionError: division by zero
"""<--
Oh, boy. This is the second time I've seen this error.
将多处理导入为mp
def do_stuff(num):
如果为假:
x=10
其他:
x=20
结果=5/(2-num)#第9行
返回结果
def success_处理程序(结果):
打印(‘成功’)
def错误处理程序(e):
打印('错误')
打印(目录(e),“\n”)
print(“-->{}我不知道e.。\uu-cause\uuuuudef-error\u-callback(self,e):print('error\u-callback()',e.。\uu-cause\uuuuuu)
提供与回溯相同的回溯。print\u异常(type(e),e,e.。\uu-traceback\uu)!关于error\u回调的目的。我不是专家,所以我不确定我是否正确理解您的问题或error\u回调的目的,但是如果您没有error\u回调,并且错误发生在目标函数中的try/except块之外,则即使引发错误,也不会捕获错误。在我的情况下,它会导致我的脚本错误非常明显地冻结或继续使用奇怪的行为,并且必须用ctrl+c中断。因此,我将error\u回调视为“except”处理程序ie try:target function except error\u callback视为e:(handle e)。我就是这样看待/使用它的。@user1267259,我将error\u callback视为“except”“处理程序--但它不是那样工作的。在我的示例中,如果我删除try/except aboutget()
,错误\u回调不会捕获错误。据我所知,我没有这个问题。我在代码中看到的与我的代码的唯一区别是,使用返回值和以后的结果\u obj.get()如果在范围(3)中为i运行:my_pool.apply_async(…)
?(而不是result=[list comp]并删除带有结果的try/except\u obj.get()