multithreading - thread getting stuck (python) -


basically have program starts new thread, tries stuff in new thread. however, new thread seems stuck until main thread reaches end of program.

here working example of problem i'm experiencing. seems gtk.main() has there problem occur. if use input() instead, problem doesn't appear.

import threading,sys class mythread(threading.thread):     def run(self):         import time         print('before')         time.sleep(3)         print('after')  mythread().start()  gi.repository import gtk gtk  class my_window:     def __init__(self):         self.window = gtk.window()         self.window.connect("delete_event", gtk.main_quit)         self.button = gtk.button("hello world")         self.window.add(self.button)         self.button.show()         self.window.show()  my_window() gtk.main() 

what should happens: window appears, word before appears, , 3 seconds later word after appears

what happens: window appears, word before appears, nothing happens. when close window, word after appears. (as if somehow reaching end of program makes running threads frozen before, running again)

things i've tried:

  • replaced time.sleep other functions, terminal command, or busy loop. same results

  • i tried recreating problem without window , other stuff, couldn't. don't know problem resides, gave background info could.

  • i tried using sys.stdout.flush() make sure text showing in command line when should. was.

does have suggestions problem might be?

i'm using python 3 (and gtk window), , prefer program compatible on major os's

edit:

i tried putting pring('starting') right before gtk.main(). output showed before starting after. i'm thinking calling gtk.main freezes threads, , when gtk.main ends, threads resume.

edit 2: never-mind, in orriginal program, thread created while gtk.main running, there still in gtk.main freezing threads, being called every once in while.

i managed reproduce this, , spent ages trying work out why failing, until came across this faq entry, suggested putting...

import gobject gobject.threads_init() 

...at top of code, seems fix problem.

my guess after calling gtk.main(), gtk holding on python's global interpreter lock, bit naughty default behavior, @ least there's way prevent it.

note background threads not able manipulate gtk objects directly, you'll have have them pass messages main thread instead.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -