远程热插拔Python调试器 [英] Remote hotplug Python debugger

查看:143
本文介绍了远程热插拔Python调试器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过Python IDE集成设置可热插拔的远程调试器?例如,使用PyCharm.通过热插拔,我的意思是:可以快速连接开发服务器并与之断开连接.

How I can setup a hotpluggable remote debugger with Python IDE integration? For example using PyCharm. By hotpluggable I mean: can connect and disconnect from dev server on fly.

我在云中拥有开发服务器(django,nginx,uwsgi,postgres,debian),并使用PyCharm作为主要的IDE(但是如果您有任何其他IDE的解决方案,请提供它).

I have dev server in the cloud (django, nginx, uwsgi, postgres, debian), and use PyCharm as the main IDE (but if you have solution for any other IDE, please provide it).

有时候,我需要在不停止/重新启动开发服务器的情况下连接和调试脚本.使用PyCharm的调试器(pydevd),如果没有可用的调试器服务器( Connection拒绝),开发服务器就无法启动,并且如果我在开发服务器运行时停止了远程调试器,则会崩溃,例如

Sometimes, I need to connect and debug scripts without stopping/restarting the dev server. Using PyCharm's debugger (pydevd), dev server cannot start without working debugger server (Connection refused), and if I stop the remote debugger while the dev server is running, it crashes e.g.

An existing connection was forcibly closed by the remote host

我找到了pdg/epdg,但是它们与PyCharm没有集成.PyCharm还具有一个不错的功能:附加到进程",但仅适用于本地进程.

I found pdg/epdg but they have no integration with PyCharm. Also PyCharm has a nice feature : "Attach to process", but it works only with local processes.

推荐答案

可能可行的基本方法是为要调试的程序设置信号处理程序,以将其加载到调试器中.然后向进程发送信号进行调试(通过 kill 命令),然后可以进行远程连接.

The basic approach that would probably work is to set up a signal handler to the program you want to debug to load in a debugger. Then sending the process a signal to debug (via the kill command) you could then attach remotely.

这是通过 python trepan 现在运行:

$ python /tmp/foo.py
8530

从上面的输出中,我们很有帮助地列出了我们要调试的Python进程的pid.

From above output we helpfully listed the pid of the Python process we want to debug.

现在在外壳中,我们发送一个信号,告诉进程进入在信号处理程序中设置的调试器.您将不得不调整进程ID.

Now in a shell we send a signal to tell the process to go into the debugger that is set up in the signal handler. You will have to adjust the process id.

$ kill -USR1 8530   # Adjust the pid to what you see above

在我们运行/tmp/foo.py 的外壳中,您现在应该看到新的输出:

And in the shell where we ran /tmp/foo.py you should now see the new output:

$ python /tmp/foo.py
8530
Starting TCP server listening on port 1955. # This is new

回到我们发出 kill -USR1 的外壳程序,我们现在将在调试器中停止的进程附加到该程序中:

Back to the shell where we issued the kill -USR1, we now attach the the process now stopped inside a debugger:

$ trepan2 --client --port 1955
Connected.
(/tmp/foo.py:11 @101): signal_handler
-- 11     return
(trepan2*) list
  6         connection_opts={'IO': 'TCP', 'PORT': 1955}
  7         intf = Mserver.ServerInterface(connection_opts=connection_opts)
  8         dbg_opts = {'interface': intf}
  9         print('Starting TCP server listening on port 1955.')
 10         debug(dbg_opts=dbg_opts)
 11  ->     return
 12
 13     signal.signal(signal.SIGUSR1, signal_handler)
 14     # Go about your business...
 (trepan2*) backtrace
 ->   0 signal_handler(num=10, f=<frame object at 0x7f9036796050>)
      called from file '/tmp/foo.py' at line 11
 ##   1 <module> file '/tmp/foo.py' at line 20

这篇关于远程热插拔Python调试器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆