1

I have python application that I am running in debugging mode.

There are many async functions that i want to check in the debug mode.

So if i write

await abc()

in the debug console it throws an error

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

I tried asyncio like this

asyncio.run(abc())

and also like this

asyncio.get_event_loop().run_until_complete(abc())

but it shows

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

it is paused on debug point.

is there any way to solve this?

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189

1 Answers1

0

This is a modification of this answer: https://stackoverflow.com/a/67847333/17643448

This solution uses nest_asyncio. If one has the following async example script:

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())

Then in the debug console, you can do:

result = return_awaited_value(a)

And VSCode will return the result of running a as if it was awaited, which in this case is a json from a http request, which would return as a Python dict.

Sorry for responding 11 months late. I had the exact same problem just now and spent hours trying to figure it out. It was very frustrating and you probably solved it/moved on by now. But I wanted to answer this for future users who come to this thread for an answer.

zyxwvu
  • 21
  • 2