gpt4 book ai didi

Python HTTP 调试器

转载 作者:太空狗 更新时间:2023-10-30 01:19:00 28 4
gpt4 key购买 nike

我想设置一些调试命令(如 import ipdb; ipdb.set_trace()),以便在 jupyter 中运行调试器(我必须运行 HTTP 服务器)。有人知道这样的事情吗?

上下文:我有一个长时间运行的任务,由调度程序处理(非交互模式)。我希望能够在以相同方式运行时调试这样的任务。

最佳答案

I need to run code in "detached" (not interactive). And when some error is detected I would like to run debugger. That's why I've been thinking about remote debugger/jupyter notebook or whatever. So - by default there is no debugging session - so I think that PyCharm remote debugger is not a case.

与您在这里的想法相反,您实际上不需要在“调试 session ”中运行代码即可使用远程调试。

尝试以下操作:

  • 在 Python 环境中为您的“分离”代码安装 pydevd:

    pip install pydevd
  • 在该代码中您本应使用 pdb.set_trace 的地方,写

    import pydevd; pydevd.settrace('your-debugger-hostname-or-ip')

现在,只要您的代码遇到 pydevd.settrace 指令,它就会尝试连接到您的调试器服务器。

然后您可以从 Eclipse PyDev 或 Pycharm 中启动调试器服务器,并让“跟踪”进程连接到您准备调试。阅读here了解更多详情。

当然,在连接超时的情况下如何操作由您决定 - 您可以让您的进程在循环中永远等待调试器,或者在某个时候放弃。这是一个似乎对我有用的例子(在远程 Linux 机器上运行服务,通过远程端口转发通过 SSH 连接到它,在 Windows 下通过 Eclipse PyDev 启动本地调试服务器)

import pydevd
import socket
from socket import error

def wait_for_debugger(ex, retries=10):
print("Bam. Connecting to debugger now...")
while True:
try:
pydevd.settrace()
break
except SystemExit:
# pydevd raises a SystemExit on connection failure somewhy
retries -= 1
if not retries: raise ex
print(".. waiting ..")

def main():
print("Hello")
world = 1
try:
raise Exception
except Exception as ex:
wait_for_debugger(ex)

main()

不过,您似乎应该在启用端口转发之前启动本地调试服务器。否则 settrace 会无限挂起,显然认为它已经“连接”,但实际上并没有。

似乎还有一个名为rpcpdb 的小项目。出于类似的目的,但是我无法让它开箱即用,所以不能发表太多评论(我相信无论如何在 IDE 中单步执行代码会更方便)。

关于Python HTTP 调试器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50509148/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com