Note that there are some explanatory texts on larger screens.

plurals
  1. PODbus/GLib Main Loop, Background Thread
    primarykey
    data
    text
    <p>I'm starting out with DBus and event driven programming in general. The service that I'm trying to create really consists of three parts but two are really "server" things.</p> <p>1) The actual DBus server talks to a remote website over HTTPS, manages sessions, and conveys info the clients.</p> <p>2) The other part of the service calls a keep alive page every 2 minutes to keep the session active on the external website</p> <p>3) The clients make calls to the service to retrieve info from the service.</p> <p>I found some simple example programs. I'm trying to adapt them to prototype #1 and #2. Rather than building separate programs for both. I thought I that I can run them in a single, two threaded process. </p> <p>The problem that I'm seeing is that I call time.sleep(X) in my keep alive thread. The thread goes to sleep, but won't ever wake up. I think that the GIL isn't released by the GLib main loop. </p> <p>Here's my thread code:</p> <pre><code>class Keepalive(threading.Thread): def __init__(self, interval=60): super(Keepalive, self).__init__() self.interval = interval bus = dbus.SessionBus() self.remote = bus.get_object("com.example.SampleService", "/SomeObject") def run(self): while True: print('sleep %i' % self.interval) time.sleep(self.interval) print('sleep done') reply_status = self.remote.keepalive() if reply_status: print('Keepalive: Success') else: print('Keepalive: Failure') </code></pre> <p>From the print statements, I know that the sleep starts, but I never see "sleep done."</p> <p>Here is the main code:</p> <pre><code>if __name__ == '__main__': try: dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) session_bus = dbus.SessionBus() name = dbus.service.BusName("com.example.SampleService", session_bus) object = SomeObject(session_bus, '/SomeObject') mainloop = gobject.MainLoop() ka = Keepalive(15) ka.start() print('Begin main loop') mainloop.run() except Exception as e: print(e) finally: ka.join() </code></pre> <p>Some other observations:</p> <p>I see the "begin main loop" message, so I know it's getting control. Then, I see "sleep %i," and after that, nothing. </p> <p>If I ^C, then I see "sleep done." After ~20 seconds, I get an exception from self.run() that the remote application didn't respond:</p> <blockquote> <p>DBusException: org.freedesktop.DBus.Error.NoReply: Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.</p> </blockquote> <p>What's the best way to run my keep alive code within the server?</p> <p>Thanks,</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload