Monitoring lifetimes with Python bindings

Ole Laursen olau at
Thu Jul 7 23:02:52 EST 2005

Havoc Pennington <hp at> writes:

> On Wed, 2005-07-06 at 16:45 -0400, John (J5) Palmieri wrote:
>> @dbus.explicitly_pass_message is mainly for debugging.  Messages should
>> not be exposed in the python bindings.  The best thing to do is for your
>> client to send its unique name to the server when it connects to the bus
>> and register the NameOwnerChanged signal with sender equal to the name
>> you received.  You can also use the GetNameOwner call to get the unique
>> name of an already running process.
> I think exposing messages would be wrong but exposing the caller of a
> method may not be if we could figure out a good approach.

Perhaps one possibility would be to use another decorator, e.g.
@dbus.pass_sender, which would pass a Sender object. Sender could then
expose get_name() to return the unique name and a disappeared() hook
that you can connect to. The disappeared() hook would then internally
register with the NameOwnerChanged signal and also call GetNameOwner
to avoid the race condition.

Does this sound like a good idea?

Perhaps something like the following snippet (I'm not a Python
expert, but it appears to work):

class Sender:
    def __init__(self, unique_name, bus):
        self.unique_name = unique_name
        self.bus = bus

    def get_name(self):
        """Return the unique name of the sender."""
        return self.unique_name

    def register_disappeared_handler(self, disappear_handler, error_handler):
        """Register a handler which is called when sender disappears."""
        self.disappear_handler = disappear_handler
        self.error_handler = error_handler
        self.disappear_handler_called = False
        bus_obj = self.bus.get_object("org.freedesktop.DBus",
                             reply_handler = self._name_has_owner_reply,
                             error_handler = error_handler,
                             dbus_interface = "org.freedesktop.DBus")

    def _name_owner_changed_handler(self, name, old_name, new_name):
        if old_name == self.unique_name and new_name == "":
            if not self.disappear_handler_called:
                self.disappear_handler_called = True
    def _name_has_owner_reply(self, has_name):
        if not has_name:
            if not self.disappear_handler_called:
                self.disappear_handler_called = True

Ole Laursen

More information about the dbus mailing list