pythondbusrhythmbox

Check if Rhythmbox is running via Python


I am trying to extract information from Rhythmbox via dbus, but I only want to do so, if Rhythmbox is running. Is there a way to check if Rhythmbox is running via Python without starting it if it is not running?

Whenever I invoke the dbus code like this:

bus = dbus.Bus()
obj = bus.get_object("org.gnome.Rhythmbox", "/org/gnome/Rhythmbox/Shell")
iface = dbus.Interface(obj, "org.gnome.Rhythmbox.Shell)

and Rhythmbox is not running, it then starts it.

Can I check via dbus if Rhythmbox is running without actually starting it? Or is there any other way, other than parsing the list of currently running processes, to do so?


Solution

  • This is similar to Rosh Oxymoron's answer, but arguably neater (albeit untested):

    bus = dbus.SessionBus()
    if bus.name_has_owner('org.gnome.Rhythmbox'):
        # ...
    

    If you want to be notified when Rhythmbox starts or stops, you can use:

    def rhythmbox_owner_changed(new_owner):
        if new_owner == '':
            print 'Rhythmbox is no longer running'
        else:
            print 'Rhythmbox is now running'
    
    bus.watch_name_owner('org.gnome.Rhythmbox')
    

    See the documentation for dbus.bus.BusConnection for more details.