检查 Rhythmbox 是否正在通过 Python 运行

发布于 2024-10-11 04:43:14 字数 421 浏览 0 评论 0原文

我正在尝试通过 dbus 从 Rhythmbox 提取信息,但我只想在 Rhythmbox 正在运行时这样做。有没有办法通过 Python 检查 Rhythmbox 是否正在运行,如果它没有运行,则无需启动它?

每当我像这样调用 dbus 代码时:

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

并且 Rhythmbox 没有运行,它就会启动它。

我可以通过 dbus 检查 Rhythmbox 是否正在运行而不实际启动它吗?或者除了解析当前正在运行的进程列表之外还有其他方法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

許願樹丅啲祈禱 2024-10-18 04:43:14

这与 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')

请参阅 dbus.bus.BusConnection 文档 了解更多详细信息。

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.

故事与诗 2024-10-18 04:43:14
dbus_main_object = bus.get_object("org.freedesktop.DBus", "/")
dbus_names = dbus_main_object.ListNames(dbus_interface='org.freedesktop.DBus')
if 'org.gnome.Rhythmbox' in dbus_names:
    do_whatever()
dbus_main_object = bus.get_object("org.freedesktop.DBus", "/")
dbus_names = dbus_main_object.ListNames(dbus_interface='org.freedesktop.DBus')
if 'org.gnome.Rhythmbox' in dbus_names:
    do_whatever()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文