[英]await in debug console python vscode
我有在调试模式下运行的 python 应用程序。
我想在调试模式下检查许多异步函数。
所以如果我写
await abc()
在调试控制台中它会引发错误
Traceback (most recent call last):
File "c:\Users\sgarg\.vscode\extensions\ms-python.python-2021.8.1102490946\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_vars.py", line 419, in evaluate_expression
compiled = compile(_expression_to_evaluate(expression), '<string>', 'eval')
File "<string>", line 1
a = await abc
^
SyntaxError: invalid syntax
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\sgarg\.vscode\extensions\ms-python.python-2021.8.1102490946\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 1207, in internal_evaluate_expression_json
pydevd_vars.evaluate_expression(py_db, frame, expression, is_exec=True)
File "c:\Users\sgarg\.vscode\extensions\ms-python.python-2021.8.1102490946\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_vars.py", line 371, in new_func
return _run_with_unblock_threads(original_func, py_db, curr_thread, frame, expression, is_exec)
File "c:\Users\sgarg\.vscode\extensions\ms-python.python-2021.8.1102490946\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_vars.py", line 339, in _run_with_unblock_threads
return _run_with_interrupt_thread(original_func, py_db, curr_thread, frame, expression, is_exec)
File "c:\Users\sgarg\.vscode\extensions\ms-python.python-2021.8.1102490946\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_vars.py", line 310, in _run_with_interrupt_thread
return original_func(py_db, frame, expression, is_exec)
File "c:\Users\sgarg\.vscode\extensions\ms-python.python-2021.8.1102490946\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_vars.py", line 421, in evaluate_expression
Exec(_expression_to_evaluate(expression), updated_globals, frame.f_locals)
File "c:\Users\sgarg\.vscode\extensions\ms-python.python-2021.8.1102490946\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec
exec(exp, global_vars, local_vars)
File "<string>", line 1
SyntaxError: 'await' outside function
我试过asyncio
asyncio.run(abc())
也像这样
asyncio.get_event_loop().run_until_complete(abc())
但它显示
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\sgarg\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py", line 555, in run_until_complete
self.run_forever()
File "C:\Users\sgarg\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py", line 510, in run_forever
raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running
它在调试点暂停。
有没有办法解决这个问题?
这是对此答案的修改: https://stackoverflow.com/a/67847333/17643448
此解决方案使用nest_asyncio 。 如果有以下异步示例脚本:
import asyncio
import nest_asyncio
async def abc():
return a_json_from_a_http_request
async def foo():
a = abc()
print('') # Set this as is a breakpoint
def return_awaited_value(coroutine: asyncio.coroutine) -> Any:
loop = asyncio.get_event_loop()
result = loop.run_until_complete(coroutine)
return result
if __name__=="__main__":
loop = asyncio.get_event_loop()
nest_asyncio.apply(loop) # This part is important!
loop.run_until_complete(foo())
然后在调试控制台中,您可以执行以下操作:
result = return_awaited_value(a)
并且 VSCode 将返回运行a
的结果,就好像它已等待一样,在这种情况下,它是来自 http 请求的 json,它将作为 Python 请求返回
抱歉迟了 11 个月才回复。 我刚才遇到了完全相同的问题,并花了几个小时试图解决这个问题。 这非常令人沮丧,你现在可能已经解决了/继续前进。 但我想为将来来此线程寻求答案的用户回答这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.