Python实时编码/调试 [英] Python live coding/debugging

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

问题描述

在程序执行期间是否有一种交互式python控制台(最好是iPython),而不需要暂停主程序,并且能够检查和修改程序变量?类似于浏览器为JavaScript提供的东西。

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.

我知道关于 pdb.set_trace() IPython.embed(),但它们都暂停程序执行,并要求将它们放在程序的源代码中。

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.

对于python中的桌面游戏开发来说,这是非常有用的。

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

推荐答案

您可以使用线程

#!/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()

使用 _spawn_background_interpreter(localals())

我还没测试过,但是如果您的程序不会持续将控件打印到控制台,否则这将很可能可能会很好,否则它将全部与交互式解释器一起发送。

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.

编辑:尝试多处理解决方案:

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()

我最初避免的原因多处理是每个新进程都获得自己的PID(和stdin)。因此,我不得不将主线程的标准传递给子进程,事情从那里得到一点麻烦。 注意,python 3.2和更低版本中有一个错误,会导致追踪回溯,您可以在$ $ c $中调用 exit() c>多处理进程。这在3.3中是固定的。

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.

不幸的是,多处理代码仅在符合POSIX的系统上运行,即不在视窗。不可逾越,只需要一个涉及管道的更多涉及的解决方案。

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.

无论如何,多处理实现可能会如果您在主线程中接近100%的CPU利用率,则可以为您提供更好的性能。如果你在* nix上,尝试一下。

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.

这篇关于Python实时编码/调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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