python - Dbus/GLib Main Loop, Background Thread -


i'm starting out dbus , event driven programming in general. service i'm trying create consists of 3 parts 2 "server" things.

1) actual dbus server talks remote website on https, manages sessions, , conveys info clients.

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

3) clients make calls service retrieve info service.

i found simple example programs. i'm trying adapt them prototype #1 , #2. rather building separate programs both. thought i can run them in single, 2 threaded process.

the problem i'm seeing call time.sleep(x) in keep alive thread. thread goes sleep, won't ever wake up. think gil isn't released glib main loop.

here's 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 print statements, know sleep starts, never see "sleep done."

here 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 e:     print(e) finally:     ka.join() 

some other observations:

i see "begin main loop" message, know it's getting control. then, see "sleep %i," , after that, nothing.

if ^c, see "sleep done." after ~20 seconds, exception self.run() remote application didn't respond:

dbusexception: org.freedesktop.dbus.error.noreply: did not receive reply. possible causes include: remote application did not send reply, message bus security policy blocked reply, reply timeout expired, or network connection broken.

what's best way run keep alive code within server?

thanks,

you have explicitly enable multithreading when using gobject calling gobject.threads_init(). see pygtk faq background info.

next that, purpose you're describing, timeouts seem better fit. use follows:

# enable timer self.timer = gobject.timeout_add(time_in_ms, self.remote.keepalive) # disable timer gobject.source_remove(self.timer) 

this calls keepalive function every time_in_ms (milli)seconds. further details, again, can found @ pygtk reference.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -