Python dbus get_object设置超时

时间:2018-04-07 05:50:45

标签: python timeout dbus

代码:

def get_user_totp_status (user_name = ''):
    key_name = 'tfaEnable'

    try:
        bus = dbus.SystemBus()
    except:
        print "connect dbus error!"
        sys.exit(1)

    infopipe_obj = bus.get_object("org.freedesktop.sssd.infopipe", "/org/freedesktop/sssd/infopipe")
    ifp = dbus.Interface(infopipe_obj,dbus_interface='org.freedesktop.sssd.infopipe')
    print "get user totp status from dbus error!"

    result = ifp.GetUserAttr(user_name, [key_name])
    user_totp_status = 'True'
    if result:
        for status in result[key_name]:
            user_totp_status = status

    return user_totp_status

===========
错误:     
dbus.exceptions.DBusException:org.freedesktop.DBus.Error.TimedOut:激活org.freedesktop.sssd.infopipe超时

======

如果DBUS出现问题,那么当" get_object"时需要很长时间。如何设置超时以缩短时间?

1 个答案:

答案 0 :(得分:1)

get_objectstart_service_by_name都不支持超时。 作为解决方法,您可以在call_blocking之前直接使用get_object,如下所示:

import dbus
from _dbus_bindings import BUS_DAEMON_IFACE, BUS_DAEMON_NAME, BUS_DAEMON_PATH

bus = dbus.SystemBus()

timeout = 2
flags = 0
bus_name = "org.freedesktop.sssd.infopipe"

try:
    bus.call_blocking(BUS_DAEMON_NAME, BUS_DAEMON_PATH,
                      BUS_DAEMON_IFACE,
                      'StartServiceByName',
                      'su', (bus_name, flags), timeout=timeout)

    infopipe_obj = bus.get_object("org.freedesktop.sssd.infopipe", "/org/freedesktop/sssd/infopipe")

except dbus.exceptions.DBusException as e:
    print("The chosen bus is not available: %s", e)