代码之家  ›  专栏  ›  技术社区  ›  MikeRand

Emacs:次模式python shell显示为“滞后”

  •  6
  • MikeRand  · 技术社区  · 15 年前

    我是一个python(3.1.2)/emacs(23.2)新手,正在使用pythonware教程自学tkinter here . 相关代码粘贴在问题下方。

    问题:当我单击hello按钮(应该称为say-hi函数)时,为什么下面的python shell(即我用c-c c-c启动的shell)会等待执行say-hi print函数,直到a)单击quit按钮或b)关闭根窗口小部件?当我在空闲时尝试相同的方法时,每次单击hello按钮都会在空闲的python shell中立即生成一个打印,甚至在单击quit或close root小部件之前也是如此。

    Emacs运行python shell(与idle相比)的方式是否有一些奇怪之处,从而导致这种“滞后”行为?我注意到类似的Emacs滞后于空闲,因为我已经解决了ProjectEuler问题,但这是我见过的最清楚的例子。

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

    (setq python python命令“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()
    
    1 回复  |  直到 13 年前
        1
  •  4
  •   msw    15 年前

    我猜如果没有连接到tty,python解释器(通过c stdio)会切换到从行缓冲区进行块缓冲,直到关闭时才会刷新stdout。运行 os.isatty(1) 在“劣质的python:run shell compile”缓冲区中,返回false,从而增加了这个猜测的权重。

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

    可能会有所不同。