致命的Python错误:PyEval_RestoreThread:NULL tstate

时间:2011-09-15 16:40:02

标签: python fatal-error

有人知道这个错误意味着什么吗?

Fatal Python error: PyEval_RestoreThread: NULL tstate

在我的应用程序中,当我销毁主窗口时会打印出此错误。我正在使用多线程来同时运行不同的作业。

我真的没有任何想法是什么......

如果有人曾经遇到同样的问题,请帮助我..

以下是显示如何重现此错误的代码。 (我试图制作最小的代码)

#!/usr/bin/env python

import gtk
import threading
import sys

class Test(threading.Thread):
    """A subclass of threading.Thread, with a kill() method."""

    def __init__(self, *args, **keywords):
        threading.Thread.__init__(self, *args, **keywords)
        gtk.gdk.threads_init()
        self.killed = False

    def start(self):
        """Start the thread."""
        self.__run_backup = self.run
        self.run = self.__run # Force the Thread to install our trace.
        threading.Thread.start(self)

    def __run(self):
        """Hacked run function, which installs the trace."""
        sys.settrace(self.globaltrace)
        self.__run_backup()
        self.run = self.__run_backup

    def globaltrace(self, frame, why, arg):
        if why == 'call':
            return self.localtrace
        else:
            return None

    def localtrace(self, frame, why, arg):
        if self.killed:
            if why == 'line':
                raise SystemExit()
                return self.localtrace

    def kill(self):
        self.killed = True

class Window(gtk.Window):
    """Main window"""
    def __init__(self):
        """Create a main window and all your children"""
        super(Window, self).__init__()
        self.connect('destroy', gtk.main_quit)
        button = gtk.Button("Click and after, close window")
        button.connect("clicked", self.on_item_run)
        self.add(button)
        self.show_all()

    def on_item_run(self, widget):
        t = Test()
        t.start()

if __name__ == "__main__":
    window = Window()

    gtk.gdk.threads_enter()
    gtk.main()
    gtk.gdk.threads_leave() 

非常感谢..

1 个答案:

答案 0 :(得分:1)

您使用的是什么版本的gtk? This link似乎表明它是在2.0.1中修复的线程错误。