server.py: use Timer threads instead of single-threaded GLib timeouts
authorDavid Adam (zanchey) <[email protected]>
Wed, 30 Sep 2009 06:08:03 +0000 (14:08 +0800)
committerDavid Adam (zanchey) <[email protected]>
Thu, 15 Oct 2009 11:14:22 +0000 (19:14 +0800)
server.py

index 25e8ff8..64752f5 100755 (executable)
--- a/server.py
+++ b/server.py
@@ -11,6 +11,7 @@ import dbus, dbus.service
 import gobject
 from LATClient import LATClient
 from select import select
+from threading import Timer
 
 def check_LAT_service(servicename, retries=3):
     latclient = LATClient(service=servicename.upper())
@@ -56,8 +57,12 @@ class Door(dbus.service.Object):
         object_path = '/au/asn/ucc/doors/%s' % doorname
         dbus.service.Object.__init__(self, bus, object_path)
         
-        # get initial state
-        self.poll()
+        # get initial state in a new thread
+        self.timeout = Timer(0, self.poll)
+        # daemon threads will be killed when the mainloop exits
+        # the timeout threads will inherit this value
+        self.timeout.setDaemon(True)
+        self.timeout.start()
     
     def poll(self):
         try:
@@ -71,7 +76,8 @@ class Door(dbus.service.Object):
             self.status_changed(newstatus)
         
         # set up timeout again
-        gobject.timeout_add_seconds(self.interval, self.poll)
+        self.timeout = Timer(self.interval, self.poll)
+        self.timeout.start()
     
     @dbus.service.signal('au.asn.ucc.DoorInterface', signature='n')
     def status_changed(self, newstatus):
@@ -83,6 +89,7 @@ class Door(dbus.service.Object):
         return self.status
 
 if __name__ == '__main__':
+    gobject.threads_init()
     doors = ('uccdoor', 'unisfadoor', 'chdoor', 'mrdoor', 'uccpir')
     
     from dbus.mainloop.glib import DBusGMainLoop

UCC git Repository :: git.ucc.asn.au