在相关问答中here ,有人假设 emacs(23.2) 中的 python-shell 是 block 缓冲的而不是行缓冲的。推荐的修复方法是将 sys.stdout.flush() 添加到脚本中我希望 stdio 将其内容刷新到 python-shell 的位置。
有什么方法可以欺骗 python-shell(在 Windows 上的 emacs 23.2 中运行,而不是在 Linux 中运行)a) 认为它附加到 TTY 或 b) 使用行缓冲模式而不是 block 缓冲模式?我不明白为什么我能够在 IDLE 中而不是 emacs 中执行此操作。
我宁愿自定义 emacs 而不是在我的脚本中添加 sys.stdout.flush()。叫我懒惰:-)。
谢谢,
迈克
最佳答案
对于那些想知道的人,我认为讨论了相关行为here , 在 emacs "7. Subprocesses\7.3 Buffering in shells and subprocesses".
“在 shell 缓冲区中,stdout 是一个管道句柄,因此以 block 的形式进行缓冲。如果您希望程序的缓冲行为有所不同,则必须更改程序本身;您可以使用 setbuf和 setvbuf 来操纵缓冲语义。”
通过将以下内容添加到我的 init.el 来解决(有关更多详细信息,请参阅此 SO 链接 here):
(setenv "PYTHONUNBUFFERED" "x")
关于python - Emacs/Python : running python-shell in line buffered vs. block 缓冲模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2881346/