From: David Adam (zanchey) Date: Wed, 30 Sep 2009 06:08:03 +0000 (+0800) Subject: server.py: use Timer threads instead of single-threaded GLib timeouts X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=f6c11e275687aa16664c4a15f57d7be591e41c16;p=uccdoor.git server.py: use Timer threads instead of single-threaded GLib timeouts --- diff --git a/server.py b/server.py index 25e8ff8..64752f5 100755 --- 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