我真的很喜欢pudb并且结合IPython多年来我已经成功地调试了 python 代码。
例如,大多数情况下,这种组合只是有效的
# test1.py
import time
def do_something():
import pudb
pudb.set_trace()
sleep(1)
return 1
do_something()
如果你执行python test1.py
, pudb 启动并且当你按下!
IPython 交互式 shell 时启动没有问题。
但是,如果您在协程中设置跟踪,则 pudb 可以毫无问题地启动,您甚至可以毫无问题地进入await some_async_call()
调用。但是,如果您想启动交互式 shell 并按!
,则整个会话会因错误而中断。狐狸示例:
# test2.py
import asyncio
async def do_something():
import pudb
pudb.set_trace()
await asyncio.sleep(1)
return 1
asyncio.run(do_something())
$ python test2.py
pudb starts and halts before await asyncio.sleep(1), now press !
Hit Ctrl-D to return to PuDB.
In [1]:
Traceback (most recent call last):
File "test2.py", line 11, in <module>
asyncio.run(do_something())
File "/home/shaoran/anaconda/py3/envs/ivct/lib/python3.8/asyncio/runners.py", line 43, in run
return loop.run_until_complete(main)
File "/home/shaoran/anaconda/py3/envs/ivct/lib/python3.8/asyncio/base_events.py", line 599, in run_until_complete
self.run_forever()
File "test2.py", line 7, in do_something
await asyncio.sleep(1)
File "test2.py", line 7, in do_something
await asyncio.sleep(1)
...
File "/home/shaoran/anaconda/py3/envs/ivct/lib/python3.8/asyncio/base_events.py", line 554, in run_forever
raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running
Task was destroyed but it is pending!
task: <Task pending name='Task-5' coro=<Renderer.wait_for_cpr_responses.<locals>.wait_for_timeout() running at /home/shaoran/anaconda/py3/envs/ivct/lib/python3.8/site-packages/prompt_toolkit/renderer.py:505> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7fe05cd42fd0>()]>>
我怀疑这是一个 IPython 问题,因为如果我将 shell 从 IPython 更改为bpython并按!
,那么 bpython 交互式 shell 将毫无问题地启动。我一直在寻找解决方案,但似乎找不到任何有用的东西,我设法在 github #12028、#12140和#12141上找到了这些问题/拉取请求,这似乎是相关的到它,但我不知道是不是这样,或者他们是否已经被释放了。
所以我的问题是:任何人都可以重现这个错误吗?这是 pudb 还是 ipython 错误?
我使用从 anaconda 4.8.3 (linux 64bit) 和 IPython 7.13.0 安装的 python 3.8.1(使用 安装conda install ipython -c conda-forge
,但通过安装时同样适用pip
)。