Skip to main content
 首页 » 编程设计

Python:具有无限循环的控制台,超出堆栈空间

2024年10月25日9shangdawei

下面粘贴了实现。但是,每次我在控制台中实际输入内容时,都会出现此错误:

        while executing 
    "::tcl::Bgerror {out of stack space (infinite loop?)} {-code 1 -level 0 -errorcode NONE -errorinfo {out of stack space (infinite loop?) 
        while execu..." 
    error in background error handler: 
 
out of stack space (infinite loop?) 

这被另一个应用程序(Matplotlib GUI)使用,它实例化 ConsoleInput,然后为自己使用最新的用户条目。

为什么会发生此错误,我该如何修复 ConsoleInput? 有没有办法检查是否有待处理的输入,然后只读取,而不是不断循环?

import threading 
 
 
class ConsoleInput(threading.Thread): 
 
    def __init__(self): 
        threading.Thread.__init__(self) 
        self.daemon = True 
        self.exit = False 
        self.most_recent_entry = None 
 
    def run(self): 
        while not self.exit: 
            self.most_recent_entry = raw_input() 

这是使用控制台输入的 GUI 的相关部分:

def __init__(self): 
    self.console_input = ConsoleInput() 
    self.console_input.start() 
    self.run() 
 
def run(self): 
    self.figure = plt.figure(figsize=(16, 8)) 
    self.figure.canvas.set_window_title(self.title) 
    self.gen_data() 
    plt.connect('button_press_event', self.on_fig_update_event) 
    plt.connect('motion_notify_event', self.on_fig_update_event) 
    plt.tight_layout() 
    plt.show() 
 
def gen_data(self): 
    if self.console_input.most_recent_entry: 
        print self.console_input.most_recent_entry: 

请您参考如下方法:

[edit] 根据您提供的新信息...试试这个解决方案 (下面找到原始解决方案)

class SomeClass: 
    def __init__(self): 
        #this stuff is bad dont do it 
        #self.console_input = ConsoleInput() 
        #self.console_input.start() 
        self.run() 
    def on_console(self,evt): 
 
        print "data = ",self.var.get() 
 
    def run(self): 
        self.figure = plt.figure(figsize=(16, 8)) 
        self.figure.canvas.set_window_title("a title") 
        plt.subplots_adjust(bottom=0.25) # Make space for stuff 
        plt.plot([1,2,3],[5,5,6]) 
        #add stuff to our frame 
        self.var = StringVar() #hold user variable 
        b = Button(self.figure.canvas.get_tk_widget(),text="Update Chart!") 
        b.pack(side="bottom",fill="both",padx=4,pady=1) 
        slider = Entry(self.figure.canvas.get_tk_widget(),textvariable = self.var) 
        slider.pack(side="bottom", fill='both',  padx=4, pady=4) 
        b.bind("<Button-1>",self.on_console) 
        plt.show() 

两个类似的东西可能是你正在寻找的......但一般来说你真的不应该用 gui 程序阅读 stdin ...

更好的解决方案是从 gui 中获取信息,例如

from matplotlib import pyplot as plt 
import tkSimpleDialog 
class SomeClass: 
    def __init__(self): 
        #this stuff is bad dont do it 
        #self.console_input = ConsoleInput() 
        #self.console_input.start() 
        self.run() 
    def on_console(self,evt): 
        data = tkSimpleDialog.askstring("HELLO?","You?") 
        print data 
        #do something with the data (and self.figure perhaps) 
    def run(self): 
        self.figure = plt.figure(figsize=(16, 8)) 
        self.figure.canvas.set_window_title("a title") 
        plt.connect('key_release_event', self.on_console) 
        plt.show() 

然而,如果你真的想使用控制台输入,你应该使用 cmd 模块

import cmd 
from matplotlib import pyplot as plt 
class SomeClass: 
        def __init__(self): 
            #this stuff is bad dont do it 
            #self.console_input = ConsoleInput() 
            #self.console_input.start() 
            self.run() 
        def on_console(self,evt): 
            print "EVT:",evt 
            MyCmdInterface(self).cmdloop() 
        def run(self): 
            self.figure = plt.figure(figsize=(16, 8)) 
            self.figure.canvas.set_window_title("a title") 
            plt.connect('key_release_event', self.on_console) 
            plt.show() 
 
class MyCmdInterface(cmd.Cmd): 
    """Simple command processor example.""" 
    prompt="CMD:" 
    def __init__(self,guiInst): 
        print "MPL Interactive Command Interface! type HELP for help." 
        cmd.Cmd.__init__(self) 
        self.gui = guiInst 
    def do_HELP(self,line): 
        print "Some Help Message" 
    def do_SET(self, line): 
        assert "=" in line,"SET COMMANDS REQUIRE <varName>=<valName>" 
        var,val = line.split("=",1) 
        #do something 
    def do_NEWDATA(self,line): 
        newdata = map(float,line.split(",")) 
        print "SET latest_values = %s"%newdata 
        self.gui.latest_values = newdata 
        #do something with new data 
 
    def do_EOF(self, line): 
        return True 
    #aliases for exit 
    do_EXIT = do_exit = do_QUIT = do_quit = do_DONE = do_done = do_EOF 
 
if __name__ == '__main__': 
    s = SomeClass() 
    s.run() 

这也演示了一个简短的可运行代码示例是什么样的