You are not logged in.

#1 2007-08-04 01:52:30

NoOneImportant
Member
From: Deep Southern California
Registered: 2007-02-13
Posts: 178

pygtk and threading

ok so I have this program where I want to redirect everything from stdout into a TextBuffer. In addition, I am trying to accomplish this by making a thread. Here is the thread for instance.

class IOWAIT(threading.Thread):
    def __init__(self, widgets):
        threading.Thread.__init__(self)
        #class object of widgets passed to the thread
        self.widgets = widgets
        self.readmax = 200
        #decides whether the loop should continue
        self.thread_keeps_going = 1
        self.log = open("applog.txt", "w+")
        redirects stdout to applog.txt
        sys.stdout = self.log
        self.is_readable = [sys.stdout.fileno()]
        self.is_writable = []
        self.is_error = []
    def run(self):
        while(self.thread_keeps_going):
            #wait for applog.txt to have data to be read
            r,w,e = select.select(self.is_readable, self.is_writable, self.is_error)
            if r:
                #when the file has data that can be read
                gtk.gdk.threads_enter()
                self.widgets.consolebuffer.insert_at_cursor(sys.stdout.readline())
                gtk.gdk.threads_leave()

                #close file and reassign stdout its original file object
        self.log.close()
        sys.stdout = sys.__stdout__
        print "thread finished"

And yes, I did call gtk.gdk.threads_init() prior to gtk.main(), but there's a deadlock when the select() function determines that the file has readable data. Without gtk.gdk.threads_enter() and gtk.gdk.threads_leave() nothing happens in the TextBuffer either.

So what am I doing wrong?

Last edited by NoOneImportant (2007-08-04 01:55:48)

Offline

#2 2007-08-18 12:39:54

Husio
Member
From: Europe
Registered: 2005-12-04
Posts: 359
Website

Re: pygtk and threading

Just reading about the threads in pygtk... this may be helpful : http://faq.pygtk.org/index.py?req=show& … 20.001.htp

Offline

Board footer

Powered by FluxBB