如何在 python run_in_executor 方法调用中捕获异常
How to catch exceptions in a python run_in_executor method call
如何在使用 run_in_executor 调用的 run_long_thing() 函数中引发异常?
看起来像是被吞噬了一样。我不需要阻塞代码中函数的结果。它基本上是一个即发即忘的功能,但如果有任何异常,我仍然需要捕获异常...
import asyncio
import time
def fire_and_forget(task, *args, **kwargs):
loop = asyncio.get_event_loop()
if callable(task):
#if threadpoolworker is set to None,
#the max_workers will default to the number of processors on the machine, multiplied by 5
return loop.run_in_executor(None, task, *args, **kwargs)
else:
raise TypeError('Task must be a callable.')
async def run_long_thing(sleep):
print("Doing long thing... {:}".format(sleep))
time.sleep(sleep)
print("Done doing long thing. {:}".format(sleep))
raise Exception("sh*t happens")
def do_it():
print("Starting my main thing...")
print("Calling my long thing...")
for i in range(0,10,1):
try:
fire_and_forget(run_long_thing, i)
print(i)
print("Pom pi dom...")
time.sleep(0.1)
print("POOOOM Pom pi dom...")
except:
print("can i see the sh*t?")
do_it()
首先,如果您调用 time.sleep
,您将永远不会结束 运行 asyncio
事件循环,因此不会检测到任何结果。不要在 do_it
中调用 time.sleep
你最好做类似
的事情
asyncio.get_event_loop().run_until_complete(asyncio.sleep(0.1))
现在,来自 run_in_executor 的 return 是一个未来。如果您不介意编写异步定义并在 asyncio
循环中使用 create_task
,您可以执行类似
的操作
async def run_long_thing(thing, *args):
try: await asyncio.get_event_loop().run_in_executor(None, thing, *args)
except:
#do stuff
但更符合您当前的代码,您可以附加异常回调
def callback(future):
if future.exception(): #your long thing had an exception
# do something with future.exception()
然后当你调用 run_in_executor:
future = asyncio.get_event_loop().run_in_executor(None, fun, *args)
future.add_done_callback(callback)
然后 callback
将在您的执行程序任务完成时被调用。如果没有异常,future.result()
将包含结果,future.exception()
将返回任何引发的异常
如何在使用 run_in_executor 调用的 run_long_thing() 函数中引发异常? 看起来像是被吞噬了一样。我不需要阻塞代码中函数的结果。它基本上是一个即发即忘的功能,但如果有任何异常,我仍然需要捕获异常...
import asyncio
import time
def fire_and_forget(task, *args, **kwargs):
loop = asyncio.get_event_loop()
if callable(task):
#if threadpoolworker is set to None,
#the max_workers will default to the number of processors on the machine, multiplied by 5
return loop.run_in_executor(None, task, *args, **kwargs)
else:
raise TypeError('Task must be a callable.')
async def run_long_thing(sleep):
print("Doing long thing... {:}".format(sleep))
time.sleep(sleep)
print("Done doing long thing. {:}".format(sleep))
raise Exception("sh*t happens")
def do_it():
print("Starting my main thing...")
print("Calling my long thing...")
for i in range(0,10,1):
try:
fire_and_forget(run_long_thing, i)
print(i)
print("Pom pi dom...")
time.sleep(0.1)
print("POOOOM Pom pi dom...")
except:
print("can i see the sh*t?")
do_it()
首先,如果您调用 time.sleep
,您将永远不会结束 运行 asyncio
事件循环,因此不会检测到任何结果。不要在 do_it
中调用 time.sleep
你最好做类似
asyncio.get_event_loop().run_until_complete(asyncio.sleep(0.1))
现在,来自 run_in_executor 的 return 是一个未来。如果您不介意编写异步定义并在 asyncio
循环中使用 create_task
,您可以执行类似
async def run_long_thing(thing, *args):
try: await asyncio.get_event_loop().run_in_executor(None, thing, *args)
except:
#do stuff
但更符合您当前的代码,您可以附加异常回调
def callback(future):
if future.exception(): #your long thing had an exception
# do something with future.exception()
然后当你调用 run_in_executor:
future = asyncio.get_event_loop().run_in_executor(None, fun, *args)
future.add_done_callback(callback)
然后 callback
将在您的执行程序任务完成时被调用。如果没有异常,future.result()
将包含结果,future.exception()
将返回任何引发的异常