【问题标题】:Emacs: Inferior-mode python-shell appears "lagged"Emacs:劣质模式 python-shell 出现“滞后”
【发布时间】:2010-05-18 21:08:28
【问题描述】:

我是 Python(3.1.2)/emacs(23.2) 新手,使用找到的 pythonware 教程自学 tkinter here。相关代码粘贴在问题下方。

问题:当我单击 Hello 按钮(它应该调用 say_hi 函数)时,为什么劣质 python shell(即我用 Cc Cc 启动的那个)等待执行 say_hi 打印函数,直到我要么 a) 单击退出按钮或 b) 关闭根小部件?当我在 IDLE 中尝试相同的操作时,每次单击 Hello 按钮都会在 IDLE python shell 中立即打印,甚至在我单击退出或关闭根小部件之前。

emacs 运行 Python shell(与 IDLE 相比)的方式是否存在一些怪癖,会导致这种“滞后”行为?在解决 Project Euler 问题时,我注意到 emacs 与 IDLE 有类似的滞后,但这是我见过的最清晰的示例。

仅供参考:我使用 python.el 并且有一个相对干净的 init.el...

(setq python-python-command "d:/bin/python31/python")

是我的 init.el 中唯一的一行。

谢谢,

迈克

=== 开始代码===

from tkinter import *

class App:

    def __init__(self,master):

        frame = Frame(master)
        frame.pack()

        self.button = Button(frame, text="QUIT", fg="red", command=frame.quit)
        self.button.pack(side=LEFT)

        self.hi_there = Button(frame, text="Hello", command=self.say_hi)
        self.hi_there.pack(side=LEFT)

    def say_hi(self):
        print("hi there, everyone!")

root = Tk()

app = App(root)

root.mainloop()

【问题讨论】:

  • 试试print('abc', file=sys.stderr) 可能是缓冲问题(输出到控制台可能是行缓冲,但输出到管道/文件可能有固定缓冲区)。
  • 没用。 sys.stdout.flush() [msw's answer below] 工作。不过,感谢您的评论!

标签: python emacs


【解决方案1】:

我猜 Python 解释器(通过 C stdio)没有附加到 tty,从行缓冲切换到块缓冲,并且在关闭之前不会刷新 stdout。在“Inferior Python:run Shell Compile”缓冲区中运行 os.isatty(1) 会返回 false,从而增加了这个猜测的权重。

def say_hi(self):
    print("hi there, everyone!")
    sys.stdout.flush()

可能会有所作为。

【讨论】:

  • 这对打印问题有效。出于某种奇怪的原因,劣质缓冲区在退出/关闭时挂起。不过,这是一个不同的问题……非常感谢您的帮助。
  • Tkinter 中的事件循环有一些我从未深入探索过的奇怪属性。我知道 IDLE 下的 Tkinter 行为很奇怪,我猜是因为事件循环的决斗。
  • 很好的视角......当我在这方面做得更好时。再次感谢。
猜你喜欢
  • 1970-01-01
  • 2011-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多