python - How do I get all instances of VLC on dbus quickly? -


basically problem is, way instances of vlc search non-named instances org.freedesktop.mediaplayer identity function , call it.

(alternatively use introspection api, wouldn't seem solve problem) unfortunately many programs upon having sent dbus call, not respond, causing long , costly timeout.

when happens multiple times can add up. builtin timeout excessively long.

if can decrease dbus timeout somehow solve problem, ideal solution way.

i got idea put each call "identify" inside thread , kill threads take long, seems not suggested. adding multithreading increases cpu load while not increasing speed of program much.

here code trying run (more or less) painfully slow.

import dbus bus = dbus.sessionbus() dbus_proxy = bus.get_object('org.freedesktop.dbus', '/org/freedesktop/dbus') names = dbus_proxy.listnames() 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 

easier spawning bunch of threads make calls different services asynchronously, providing callback handler when result comes or d-bus error occurs. of calls happen in parallel, , program can proceed gets positive results.

here's quick-and-dirty program prints list of services finds. note how gets positive results without having wait timeouts anything. in real program you'd assign do-nothing function error handler, since goal here ignore services don't respond, example waits until it's heard before quitting.

#! /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 ()         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 () 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -