This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: threading.Timer and gtk.main are not compatible
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.6
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, eric, exarkun, pitrou
Priority: normal Keywords:

Created on 2009-05-20 20:42 by eric, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (6)
msg88137 - (view) Author: Eric (eric) Date: 2009-05-20 20:42
this simple code:
"
import gtk
from threading import Timer
from time import sleep

def p():
    print "p"

Timer(1, p).start()

#gtk.main()
sleep(10)
print "done"
does print "p" a second after it starts.
when I remove the comment of the gtk.main() line: the "p" is never printed.

It is very exposed, as Timer is a common tool to build a GUI therefore
with the gtk.main() loop active.
msg88258 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-05-24 11:44
It is probably a problem with the gtk/gobject Python binding.
By the way, you're supposed to call `gobject.threads_init()` if you use
threads with Python and gobject/gtk.
Besides, gtk certainly has its own timing facilities, while
threading.Timer is rather crude.
msg88261 - (view) Author: Eric (eric) Date: 2009-05-24 14:51
OK, this is a workaround. 

adding this line 
gtk.gdk.threads_init()

makes the job. And that's ok for me.

nevertheless, I still got the feeling that's this is a flaw in the Timer
implementation: it's behaviour is changed by some external (gtk) code.
msg88435 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2009-05-27 19:49
Well I'm not a gtk user, but try to do something else than a "print" in
the timer. Perhaps gtk redirects all standard IO or something. It sounds
very strange that threads would stop working when gtk is imported.
msg88436 - (view) Author: Jean-Paul Calderone (exarkun) * (Python committer) Date: 2009-05-27 19:51
pygtk doesn't release the GIL around its internal calls unless you call
threads_init.  So I think this is pretty clearly just a misuse of the
pygtk library.  There's nothing at all Python can do about it.  If an
extension library doesn't release the GIL, no Python code can run
concurrently with it.  In this case, the extension API you've invoked is
the Gtk main loop, which runs until you stop it, usually right before
your gtk app exits.
msg88468 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2009-05-28 08:24
When using gtk and threads, it's necessary to call
   gtk.gdk.threads_init()
History
Date User Action Args
2022-04-11 14:56:49adminsetgithub: 50323
2009-05-28 08:24:07amaury.forgeotdarcsetstatus: open -> closed

nosy: + amaury.forgeotdarc
messages: + msg88468

resolution: works for me
2009-05-27 19:51:39exarkunsetnosy: + exarkun
messages: + msg88436
2009-05-27 19:49:45pitrousetmessages: + msg88435
2009-05-24 14:51:59ericsetstatus: pending -> open

messages: + msg88261
2009-05-24 11:44:05pitrousetstatus: open -> pending
nosy: + pitrou
messages: + msg88258

2009-05-20 20:42:29ericcreate