Created
February 2, 2009 04:05
-
-
Save nzjrs/56776 to your computer and use it in GitHub Desktop.
Python example demonstrating when callbacks are run in a threaded environment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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