Python live coding/debugging

匿名 (未验证) 提交于 2019-12-03 02:05:01

问题:

Is there a way to spawn an interactive python console (preferably iPython) during program execution without pausing main program and be able to check and modify program variables? Something similar to what browsers offer for JavaScript.

I know about pdb.set_trace() and IPython.embed(), but both of them pause program execution and require to place them somewhere in source code of the program.

This would be extremaly useful for desktop game development in python.

回答1:

You could roll-your-own somewhat with threading:

#!/usr/bin/python3  def _spawn_background_interpreter(*args,**kwargs):     from threading import Thread     def _open_interp(locs):         import code         code.interact(local=locs)     locs = args[0] if args else None     t = Thread(target=_open_interp, args=(locs,))     t.setDaemon(True) #pre-3.3 API     t.start() 

Call with _spawn_background_interpreter(locals()).

I haven't tested it, but this will probably be fine if your program doesn't continuously print things to the console - otherwise it will be all munged together with the interactive interpreter.

The "opening a new console" idea is interesting, but very environment-specific, so I won't tackle that. I would be interested if there's a better prepackaged solution out there.

Edit: an attempt at a multiprocessing solution:

def _spawn_background_interpreter(*args,**kwargs):     from multiprocessing import Process     import sys, os     def _open_interp(locs,stdin):         import code         sys.stdin = os.fdopen(stdin)         code.interact(local=locs)     locs = args[0] if args else None     fileno = sys.stdin.fileno()     p = Process(target=_open_interp, args=(locs,fileno))     p.daemon = True     p.start() 

The reason I initially avoided multiprocessing is that each new process gets its own PID (and stdin). Thus, I had to pass the main thread's stdin to the child process, and things get a little hacky from there. NOTE that there is a bug in python 3.2 and lower that will cause tracebacks to spew any time you call exit() in a multiprocessing process. This is fixed in 3.3.

Unfortunately, the multiprocessing code only runs on POSIX-compliant systems - i.e. not on Windows. Not insurmountable, just going to require a more involved solution involving pipes.

Anyway the multiprocessing implementation is likely going to perform better for you if you're approaching 100% CPU utilization in your main thread. Give it a try if you're on *nix.



回答2:

I presume you are familiar with PyDev running under Eclipse. This provides excellent debugging capabilities if you are prepared to interrupt your main program (which is not what you want). However PyDev has a feature called running a Debug Server which allows attaching to an existing running program. I haven't tried it myself, but suggest you look at it, because PyDev is an excellent plugin for Eclipse. If you try it, I'd be interested in your thoughts. Try looking at:

http://pydev.org/manual_adv_remote_debugger.html 

for details. Good luck.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!