在Python调试器中是否可以await任意调用async函数?
说我在某些main.py文件中有以下代码:
import asyncio
async def bar(x):
return x + 1
async def foo():
import ipdb; ipdb.set_trace()
asyncio.run(foo())
Run Code Online (Sandbox Code Playgroud)
现在,我想bar()在调试器中测试带有某些参数的调用以测试结果。发生以下情况:
$ python3 main.py
> /Users/user/test/main.py(8)foo()
7 import ipdb; ipdb.set_trace()
----> 8 return None
9
ipdb> bar(1)
<coroutine object bar at 0x10404ae60>
main.py:1: RuntimeWarning: coroutine 'bar' was never awaited
import asyncio
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
ipdb> await bar(1)
*** SyntaxError: 'await' outside function
Run Code Online (Sandbox Code Playgroud)
当然,我可以通过在自己的x = await bar(1)之上 …