基本上问题是,获取所有 VLC 实例的唯一方法是在所有未命名的实例中搜索 org.freedesktop.MediaPlayer 标识函数并调用它。
(或者,我可以使用自省 API,但这似乎不能解决我的问题)不幸的是,许多程序在发送 dbus 调用后,根本不响应,从而导致长时间且代价高昂的超时。
当这种情况发生多次时,它可以加起来。基本上内置超时时间过长。
如果我能以某种方式减少 dbus 超时,那将解决我的问题,但理想的解决方案将是一种方法。
我的想法是,我可以将每个对“Identify”的调用放在一个线程中,并且我可以杀死需要太长时间的线程,但这似乎不被建议。此外,添加多线程会大大增加 CPU 负载,而不会大幅提高程序的速度。
这是我试图快速运行的代码(或多或少),目前非常缓慢。
import dbus
bus = dbus.SessionBus()
dbus_proxy = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
names = dbus_proxy.ListNames()
for name in names:
if name.startswith(':'):
try:
proxy = bus.get_object(name, '/')
ident_method = proxy.get_dbus_method("Identity",
dbus_interface="org.freedesktop.MediaPlayer")
print ident_method()
except dbus.exceptions.DBusException:
pass