Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dbus/GLib Main Loop, Background Thread

Tags:

python

glib

dbus

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.

1) The actual DBus server talks to a remote website over HTTPS, manages sessions, and conveys info the clients.

2) The other part of the service calls a keep alive page every 2 minutes to keep the session active on the external website

3) The clients make calls to the service to retrieve info from the service.

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.

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.

Here's my thread 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')

From the print statements, I know that the sleep starts, but I never see "sleep done."

Here is the main 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()

Some other observations:

I see the "begin main loop" message, so I know it's getting control. Then, I see "sleep %i," and after that, nothing.

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:

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.

What's the best way to run my keep alive code within the server?

Thanks,

like image 224
fandingo Avatar asked Oct 12 '11 13:10

fandingo


1 Answers

You have to explicitly enable multithreading when using gobject by calling gobject.threads_init(). See the PyGTK FAQ for background info.

Next to that, for the purpose you're describing, timeouts seem to be a better fit. Use as follows:

# Enable timer
self.timer = gobject.timeout_add(time_in_ms, self.remote.keepalive)
# Disable timer
gobject.source_remove(self.timer)

This calls the keepalive function every time_in_ms (milli)seconds. Further details, again, can be found at the PyGTK reference.

like image 89
jro Avatar answered Sep 20 '22 22:09

jro