gpt4 book ai didi

Python dbus get_object 设置超时

转载 作者:太空宇宙 更新时间:2023-11-04 04:45:39 24 4
gpt4 key购买 nike

代码:

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”的时候会花很长时间。如何设置超时来缩短时间?

最佳答案

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

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)

关于Python dbus get_object 设置超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49704350/

24 4 0
文章推荐: c - 是否可以随我的应用程序分发更新版本的 libc?
文章推荐: linux - 将内容添加到行尾
文章推荐: html -