bus_name_has_owner in python
hp at redhat.com
Sat Mar 17 09:49:43 PDT 2007
Tony Houghton wrote:
> I'm writing a python program which can be launched multiple times, but
> only ever runs one instance of itself (a bit like, say, gnome-terminal
> does). So I want it to check whether its dbus service exists and if so
> use it to get the original instance to open another window then exit; if
> the service doesn't exist it starts it itself as well as opening a
> window, and stays running in the background.
> I was using bus_name_has_owner() but it's now deprecated in dbus-python
> 0.80.x because it's in the bindings module. What should I use instead?
> Perhaps get a list of names from the session bus and check whether the
> one I want is among them?
1) you should not use bus_name_has_owner() for this because you're
creating a race condition. Instead, you need to write the code as follows:
- try to request the name
- if the request *fails* due to an existing owner, then exit
- otherwise, you are the unique instance and can continue
This guarantees that you either have the name or someone else does.
Also, this way uses fewer round trips and is thus a bit faster.
It is also "traditional" to have a "--replace" command line option,
which means when requesting the name to specify the "replace existing"
flag. This is mostly useful when debugging (you don't have to manually
exit the old instance, just run a new one with --replace). But it may
have other uses. To do this you need the app to exit anytime it loses
the bus name, which means listening for the NameLost signal.
2) if you did want to use bus_name_has_owner() (which you don't) you
would just create a proxy object for the org.freedesktop.DBus bus name
and interface, and call methods on that proxy
More information about the dbus