[patch] Python-dbus finish implementing deferred methods

tsuraan tsuraan at gmail.com
Mon Jan 15 12:16:07 PST 2007


> I'm not sure that I understood that. Could you provide a simple test case
> that
> should work, but doesn't, and describe what it ought to do?


Ok, this is a little ugly, but it's as simple as I can get it...

tsuraan at localhost ~/test/python/dbus/blocking $ cat Slow.py
import gobject
import dbus
import dbus.service
import dbus.glib
import time

class Slow(dbus.service.Object):
  def __init__(self):
    bus = dbus.SystemBus()
    bus_name = dbus.service.BusName("com.example.Slow", bus)

    super(Slow, self).__init__(bus_name, "/Object")

  @dbus.service.method("com.example.Slow")
  def sfunction(self):
    # Sleep a while, but not long enough for dbus timeout...
    time.sleep(5)
    return 'Hi!'


if __name__ == "__main__":
  gobject.threads_init()
  dbus.glib.init_threads()

  s = Slow()
  gobject.MainLoop().run()

tsuraan at localhost ~/test/python/dbus/blocking $ cat Caller.py
import gobject
import dbus
import dbus.service
import dbus.glib
import time
import threading

class Caller(dbus.service.Object):
  def __init__(self):
    self.bus = dbus.SystemBus()
    bus_name = dbus.service.BusName("com.example.Caller", self.bus)

    super(Caller, self).__init__(bus_name, "/Object")

  @dbus.service.method("com.example.Caller")
  def cfunction(self):
    obj = self.bus.get_object("com.example.Slow", "/Object")
    thread = threading.Thread(target=obj.sfunction)
    thread.start()
    # Sleep for a moment to make the context switch happen
    time.sleep(1)
    return 'done'



if __name__ == "__main__":
  gobject.threads_init()
  dbus.glib.init_threads()

  c = Caller()
  gobject.MainLoop().run()

tsuraan at localhost ~/test/python/dbus/blocking $ cat test.py
import dbus
import dbus.glib
import time

bus = dbus.SystemBus()
obj = bus.get_object("com.example.Caller", "/Object")
start = time.time()
obj.cfunction()
print 'call took', time.time()-start, 'seconds'


tsuraan at localhost ~/test/python/dbus/blocking $ python Slow.py & python
Caller.py &
[1] 8636
[2] 8637
tsuraan at localhost ~/test/python/dbus/blocking $ python test.py
call took 5.00829911232 seconds


One would expect the call to have taken about one second, but instead it
takes five.  It looks like the first dbus method in Caller cannot return
until its call has completed.  Is that a bug, or is that a part of the
spec?  Or, is this a glib problem, and I should ask them about it?  It
doesn't really look like glib and python threads play very nicely together,
so maybe that's the problem.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freedesktop.org/archives/dbus/attachments/20070115/58dbcc5f/attachment.html


More information about the dbus mailing list