How to quickly get all VLC instances on dbus?

basically the problem is that the only way to get all the VLC instances is to look for all unnamed instances for the org.freedesktop.MediaPlayer authentication function and call it.

(alternatively, I could use the introspection API, but this did not seem to solve my problem) Unfortunately, many programs sending a dbus call simply do not respond, causing a long and expensive timeout.

If this happens several times, it can add up. Basically, the built-in timeout is too long.

If I can reduce the dbus timeout in some way, this will solve my problem, but a way would be the ideal solution.

I got the idea that I can put every call in the "Identify" inside the thread and so that I can kill threads that take too much time, but this does not seem to be suggested. In addition, the addition of multithreading significantly increases the load on the processor without increasing the speed of the program.

here is the code I'm trying to run fast (more or less) that is currently painfully slow.

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
+3
source share
1 answer

, , , , D-Bus. , , .

- , . , , - -. , , do-nothing , , , , , .

#! /usr/bin/env python

import dbus
import dbus.mainloop.glib
import functools
import glib

class VlcFinder (object):
    def __init__ (self, mainloop):
        self.outstanding = 0
        self.mainloop = mainloop

        bus = dbus.SessionBus ()
        dbus_proxy = bus.get_object ("org.freedesktop.DBus", "/org/freedesktop/DBus")
        names = dbus_proxy.ListNames ()
        for name in dbus_proxy.ListNames ():
            if name.startswith (":"):
                proxy = bus.get_object (name, "/")
                iface = dbus.Interface (proxy, "org.freedesktop.MediaPlayer")
                iface.Identity (reply_handler = functools.partial (self.reply_cb, name),
                                error_handler = functools.partial (self.error_cb, name))
                self.outstanding += 1

    def reply_cb (self, name, ver):
        print "Found {0}: {1}".format (name, ver)
        self.received_result ()

    def error_cb (self, name, msg):
        self.received_result ()

    def received_result (self):
        self.outstanding -= 1
        if self.outstanding == 0:
            self.mainloop.quit ()

if __name__ == "__main__":
    dbus.mainloop.glib.DBusGMainLoop (set_as_default = True)
    mainloop = glib.MainLoop ()
    finder = VlcFinder (mainloop)
    mainloop.run ()
+1

Source: https://habr.com/ru/post/1766493/


All Articles