Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Interactive shell to a running Python process (github.com/amoffat)
146 points by daenz on Dec 11, 2011 | hide | past | favorite | 22 comments


If you want to do the same without modifying the script (or if you have the script already running without the needed import for the remote debugging possibility), i.e. inject into a running process:

https://github.com/albertz/pydbattach


I use ipython's "ipdb" or plain old Python "pdb" for the same functionality, and a bit more -- people don't seem to realize that ipdb gives them a full-on REPL within the calling context.

I usually wrap it into our error logging functions, so an environment variable enables debugging where I would normally have a catch-all -- very handy when looking for heisenbugs in AndBug.

AntiLog -- https://gist.github.com/a256ed1295619fad7cfc

AndBug -- https://github.com/swdunlop/andbug


My most excellent friend Thomas Hurst implemented a remote python shell a few years ago, for use in debugging running instances of Terminator. You can see the code in question here:

http://bazaar.launchpad.net/~gnome-terminator/terminator/tru...

and how it gets set up, starting at line 88, here:

http://bazaar.launchpad.net/~gnome-terminator/terminator/tru...


You can also run an RPyC* server in your process, and connect to it from any Python shell on any computer to control it.

* http://rpyc.sourceforge.net/


Would it be possible to use gdb or something similar to inject this into a running process?


Yes, pyrasite does just that with a python wrapper around the gdb commands.

https://fedorahosted.org/pyrasite/

It was discussed here:

http://news.ycombinator.com/item?id=3014484


See the albertzeyer comment.


Zope has this facility and boy it was useful for troubleshooting live systems.


And Twisted has Manhole, but for some reason we were never allowed to enable it on production deployments...


What are the consequences of running this in production? What's the performance hit like, is it unsafe, etc.?


> is it unsafe, etc.?

As the readme notes, it's completely unsafe if you perform any mutation operation.

As to the performance hit, I'd expect almost none when not in use for long-running processes: it spawns a thread, and that thread will then wait on a socket accept, no connection, no resources spent. You'll still be paying in memory for the server thread, but that's it (the server thread only creates two objects, an rlcompleter.Completer instance and a function).


We've been using rfoo to do this. It doesn't seem to impact performance when you're not using it. When you are using it, it's unsafe if you do unsafe things. We primarily use it for debugging by passively inspecting the messed-up portion of the server's state.


I wanted something like this back in the day to be able to poke around in the EVE Online client. Nice job!


Very interesting. I would love to see this for other programming languages, especially Ruby (probably fairly easy) or Objective-C (may be quite hard).


It's dead easy in Node using the REPL module (http://nodejs.org/docs/v0.6.5/api/repl.html). I've used this to debug weird async issues.


Objective-C also has Cycript at http://cycript.org/, which has a hybrid syntax of JavaScript and Objective-C and supports injecting into any running process.b


Ruby's had the much more featureful Pry for some time now: http://github.com/pry/pry.

(And there have been various projects to provide some sub-capabilities for years.)


For ObjC:

https://github.com/albertz/Pyjector (and then access the ObjC runtime via the Python ObjC bridge)

https://github.com/albertz/FScriptAnywhereSIMBL


Or Cycript, which all of us iOS hackers use. ;P

http://www.cycript.org/ http://iphonedevwiki.net/index.php/Cycript


With Perl you would use Enbugger & gdb using these instructions: https://metacpan.org/module/Enbugger#From-gdb


Id like to see it in bash, all i know now is to use the +x option but you need to do it before its run


This is great I thought Ipython was great. But this is awesome.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: