[KIU Shueng Chuan]
>Hi, I was trying threads for the first time and not getting it to work, I 
>browsed the list archives and found the below piece of code. It 
>didn't work either and displayed the same symptoms as my 
>program, ie my thread didn't seem to be getting any timeslice at 
>all, but once I quit the mainloop(), the thread would jump to life.

it cannot work: the code is blocking the thread by calling threads_enter() 
and threads_leave() around mainloop() which locks the mutex that the
thread is waiting for by calling threads_enter() itself, so it can only
run after mainloop() has returned and threads_leave() has been called (at
the end of the code).

>Finally, I added an idlefunc which I commented out below, and 
>suddenly everything worked, both my program and one below.

this probably means that gdk is unlocking the mutex 
before/after/around calling an idle function. 

read the gdk/gtk docs about when to lock/unlock the gdk mutex; the
multithreading precautions should apply to a python program the same way
they apply to a C program.

>I am using win98, is this normal? 

sadly, yes ;)

and to answer your question the way you probably rather meant it: 
on unix, the code behaves the way you describe, too.

>from gtk import *
>from threading import *
>import time
>
>class Worker(Thread):
>    def __init__ (self, widget):
>        Thread.__init__(self)
>        self.counter = 0
>        self.widget = widget
>
>    def run (self):
>        while 1:
>            threads_enter()    

here it tries locking the mutex which is held by (see below)

>            self.widget.set_text ("Count: %d" % self.counter)
>            threads_leave()
>            time.sleep(1)
>            self.counter = self.counter + 1
>
># def idlefunc():
>#   time.sleep(0.1)
>#   return TRUE
>
># idle_add(idlefunc)
>
>win = GtkWindow()
>label = GtkLabel()
>win.add(label)
>win.show_all()
>
>threads_enter()

this locks the mutex.

>worker = Worker(label)
>worker.start()
>mainloop()
>threads_leave()        

this unlocks it, so the thread can lock it (and finally do its work)

.tim




_______________________________________________
pygtk mailing list   [EMAIL PROTECTED]
http://www.daa.com.au/mailman/listinfo/pygtk

Reply via email to