为什么asnycio中的异常延迟或根本没有出现?

2024-04-25 10:19:30 发布

您现在位置:Python中文网/ 问答频道 /正文

有时在使用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语法异常可以被捕获:

^{pr2}$

所以。我能立即看到异常而不被抓住吗?或者我只能使用步骤来调试?在


Tags: self程序asyncmydefwith语法await
1条回答
网友
1楼 · 发布于 2024-04-25 10:19:30

引发异常,展开堆栈。在

真正的问题是:你用什么来管理你的合作计划?在

loop.run_until_complete(my_func())将按预期处理异常。另一种使用场景可能有所不同。在

相关问题 更多 >