有时在使用async/await
语法后,我看到程序不再正常工作。但是也没有任何例外。例如:
async def my_func(self):
async with self.engine() as conn:
print('step1') # step1 shows in console
await conn.exceute("INSERT INTO bla-bla")
print('step2') # I can't watch 'step2', and no any exceptions caughted to console
但是,如果我使用try/except
语法异常,则可以捕获:
async def my_func(self):
async with self.engine() as conn:
print('step1') # step1 shows in console
try:
await conn.exceute("INSERT INTO bla-bla")
except Exception as e:
print_exc() # only by this way I can see whats wrong
print('step2')
所以。我可以立即看到异常而不被抓住吗?或者我只能使用步骤并调试所有内容?
引发异常,展开堆栈。
真正的问题是:您使用什么来运行协程?
loop.run_until_complete(my_func())
将按照您的期望处理异常。其他使用方案可能会有所不同。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句