如何在 pdb 中等待协程 [英] How to await a coroutine in pdb

查看:95
本文介绍了如何在 pdb 中等待协程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用异步库(asyncpg)并且我想调试一些异步调用以进行查询数据库.

I'm using an async library (asyncpg) and I want to debug some async calls to query the database.

我放置了一个 pdb 断点并想尝试一些查询:

I place a pdb breakpoint and want try out a few queries:

(pdb) await asyncpg.fetch("select * from foo;")
*** SyntaxError: 'await' outside function

能够做到这一点真是太好了,因为它可以让我尝试一些 SQL 查询并查看结果,所有这些都可以在我的调试器中轻松完成.

It would be great to be able to do this because it would allow me to try out a few SQL queries and see the result, all from the comfort of my debugger.

有可能吗?

推荐答案

我们不能直接调试协程(因为,假设我们必须将 pdb 放在 asyncpg.fetch 函数中的某处覆盖它).

We can't directly debug the coroutine (since, imagine we have to put the pdb inside the asyncpg.fetch function somewhere by overriding it).

相反,一种可能性是我们可以使用诸如 await 之类的任何包来创建同步函数并将其转换为异步函数 和和 pdb:

Instead, one possibility is we can make a sync function and convert it to async function using any package like awaits and and pdb:

导入异步从 awaits.awaitable 导入等待

import asyncio from awaits.awaitable import awaitable

@awaitable 
def overrided_fectch_function( a ,  b ): 
  print(a)
  import pdb; pdb.set_trace()
  return  a  +  b
  print(b)

# Now sum is a coroutine! While it is running in a separate thread, control is passed to the event-loop. 
print ( asyncio . run ( overrided_fectch_function ( 2 ,  2 )))

这篇关于如何在 pdb 中等待协程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆