检查Rhythmbox是否通过Python运行
作者:互联网
我试图通过dbus从Rhythmbox中提取信息,但我只想这样做,如果Rhythmbox正在运行.有没有办法检查Rhythmbox是否通过Python运行而不启动它如果没有运行?
每当我调用这样的dbus代码时:
bus = dbus.Bus()
obj = bus.get_object("org.gnome.Rhythmbox", "/org/gnome/Rhythmbox/Shell")
iface = dbus.Interface(obj, "org.gnome.Rhythmbox.Shell)
并且Rhythmbox没有运行,然后启动它.
如果Rhythmbox在没有实际启动的情况下运行,我可以通过dbus进行检查吗?或者除了解析当前正在运行的进程列表之外,还有其他方法吗?
解决方法:
这与Rosh Oxymoron的答案相似,但可以说是更整洁(尽管未经测试):
bus = dbus.SessionBus()
if bus.name_has_owner('org.gnome.Rhythmbox'):
# ...
如果您希望在Rhythmbox启动或停止时收到通知,您可以使用:
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')
有关详细信息,请参阅documentation for dbus.bus.BusConnection.
标签:python,dbus,rhythmbox 来源: https://codeday.me/bug/20190531/1188891.html