Emacs: Inferior-mode python-shell appears lagged

2024/12/9 22:14:32

I'm a Python(3.1.2)/emacs(23.2) newbie teaching myself tkinter using the pythonware tutorial found here. Relevant code is pasted below the question.

Question: when I click the Hello button (which should call the say_hi function) why does the inferior python shell (i.e. the one I kicked off with C-c C-c) wait to execute the say_hi print function until I either a) click the Quit button or b) close the root widget down? When I try the same in IDLE, each click of the Hello button produces an immediate print in the IDLE python shell, even before I click Quit or close the root widget.

Is there some quirk in the way emacs runs the Python shell (vs. IDLE) that causes this "lagged" behavior? I've noticed similar emacs lags vs. IDLE as I've worked through Project Euler problems, but this is the clearest example I've seen yet.

FYI: I use python.el and have a relatively clean init.el...

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

is the only line in my init.el.

Thanks,

Mike

=== Begin Code===

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

I'd guess that not being attached to a tty, the Python interpreter (via C stdio) switches to block buffered from line buffered and doesn't flush stdout until it closes. Running os.isatty(1) in an "Inferior Python:run Shell Compile" buffer returns false, thus adding weight to this guess.

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

May make a difference.

https://en.xdnf.cn/q/72772.html

Related Q&A

AttributeError: module spacy has no attribute load

import spacy nlp = spacy.load(en_core_web_sm)**Error:** Traceback (most recent call last):File "C:\Users\PavanKumar\.spyder-py3\ExcelML.py", line 27, in <module>nlp = spacy.load(en_core…

No module named Win32com.client error when using the pyttsx package

Today, while surfing on Quora, I came across answers on amazing things that python can do. I tried to use the pyttsx Text to Speech Convertor and that gave me an No module named Win32com.client error.T…

Python: How to create and use a custom logger in python use logging module?

I am trying to create a custom logger as in the code below. However, no matter what level I pass to the function, logger only prints warning messages. For example even if I set the argument level = log…

Flask-Mail - Sending email asynchronously, based on Flask-Cookiecutter

My flask project is based on Flask-Cookiecutter and I need to send emails asynchronously.Function for sending email was configured by Miguel’s Tutorial and sending synchronously works fine, but i don’…

Change text_factory in Django/sqlite

I have a django project that uses a sqlite database that can be written to by an external tool. The text is supposed to be UTF-8, but in some cases there will be errors in the encoding. The text is fro…

Shuffle patches in image batch

I am trying to create a transform that shuffles the patches of each image in a batch. I aim to use it in the same manner as the rest of the transformations in torchvision: trans = transforms.Compose([t…

Python looping: idiomatically comparing successive items in a list

I need to loop over a list of objects, comparing them like this: 0 vs. 1, 1 vs. 2, 2 vs. 3, etc. (Im using pysvn to extract a list of diffs.) I wound up just looping over an index, but I keep wondering…

Geodesic buffering in python

Given land polygons as a Shapely MultiPolygon, I want to find the (Multi-)Polygon that represents the e.g. 12 nautical mile buffer around the coastlines.Using the Shapely buffer method does not work si…

jinja2 variables naming - Are variables naming restrictions the same as for Python variables?

I didt find it written explicitly in the docs.Are the naming rules the same as with Python variables?(eg: {{ a_variablelike_that }} doesnt work for example)

What does t /= d mean? Python and getting errors

// t: current time, b: begInnIng value, c: change In value, d: durationdef: easeOutQuad, swing: function (x, t, b, c, d) {//alert(jQuery.easing.default);return jQuery.easing[jQuery.easing.def](x, t, b,…