Skip to content

Instantly share code, notes, and snippets.

@nzjrs
Created February 2, 2009 04:05
Show Gist options
  • Save nzjrs/56776 to your computer and use it in GitHub Desktop.
Save nzjrs/56776 to your computer and use it in GitHub Desktop.
Python example demonstrating when callbacks are run in a threaded environment
#!/usr/bin/env python
# Python example demonstrating when callbacks are run in a threaded environment
# John Stowers
import threading
import thread
import time
import gobject
import gtk
gtk.gdk.threads_init()
class Test(threading.Thread, gobject.GObject):
__gsignals__ = {
"now" : (gobject.SIGNAL_RUN_FIRST, None, (str,str)),
"idle" : (gobject.SIGNAL_RUN_FIRST, None, (str,str))
}
def __init__(self):
threading.Thread.__init__(self)
gobject.GObject.__init__(self)
def run(self):
print "run"
while 1:
self.emit("now", "now", str(thread.get_ident()))
gobject.idle_add(self.emit, "idle", "idle", str(thread.get_ident()))
time.sleep(1)
def cb(sender, how, i):
print "%s cb %s vs thread %s" % (how, thread.get_ident(), i)
def tick():
print "ml %s ..." % thread.get_ident()
return True
gobject.timeout_add_seconds(1, tick)
t = Test()
t.connect("now", cb)
t.connect("idle", cb)
t.start()
gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment