我正在开发一个 Python 应用程序,它将以各种形式(Flatpak、PyInstaller 可执行文件,也许还有其他)分发给 Linux 上的用户。在这个应用程序中,我想调用dbus 接口Inhibit
上的方法org.freedesktop.login1.Manager
。由于相对复杂/多样化的分发要求,我选择了 dbus-fast,它是 dbus 的纯 Python 实现。此外,dbus-python 的作者在库文档的开头就给出了各种警告,这是 dbus-fast 的另一个优点。
无论如何,dbus-fast 似乎总体上运行良好,并且我能够ListUsers
毫无问题地调用该方法,如下所示:
import asyncio
from dbus_fast import BusType
from dbus_fast.aio import MessageBus
async def list_users() -> None:
system_bus = await MessageBus(bus_type=BusType.SYSTEM).connect()
introspection = await system_bus.introspect(
"org.freedesktop.login1",
"/org/freedesktop/login1",
)
login_object = system_bus.get_proxy_object(
"org.freedesktop.login1",
"/org/freedesktop/login1",
introspection,
)
login_manager_interface = login_object.get_interface(
"org.freedesktop.login1.Manager",
)
user_list = await login_manager_interface.call_list_users()
print(user_list)
asyncio.run(list_users())
然后我可以运行并获取如下列表:
$ python -m dbus_list_users
[[1000, 'newbyte', '/org/freedesktop/login1/user/_1000']]
但是,如果我尝试调用该Inhibit
方法,则会得到EOFError
,我不明白在这种情况下它的含义。以下是代码:
import asyncio
from dbus_fast import BusType
from dbus_fast.aio import MessageBus
async def do_inhibit() -> None:
system_bus = await MessageBus(bus_type=BusType.SYSTEM).connect()
introspection = await system_bus.introspect(
"org.freedesktop.login1",
"/org/freedesktop/login1",
)
login_object = system_bus.get_proxy_object(
"org.freedesktop.login1",
"/org/freedesktop/login1",
introspection,
)
login_manager_interface = login_object.get_interface(
"org.freedesktop.login1.Manager",
)
inhibit_fd = await login_manager_interface.call_inhibit(
"sleep",
"Stack Overflow example man",
"To demonstrate that it does not work",
"delay",
)
print(inhibit_fd)
asyncio.run(do_inhibit())
运行它给了我这个很长的回溯:
$ python -m dbus_inhibit
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "/mnt/storage/Programming/sparvio_toolbox/dbus_inhibit.py", line 28, in <module>
asyncio.run(do_inhibit())
File "/usr/lib64/python3.12/asyncio/runners.py", line 194, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "/usr/lib64/python3.12/asyncio/runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib64/python3.12/asyncio/base_events.py", line 687, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "/mnt/storage/Programming/sparvio_toolbox/dbus_inhibit.py", line 20, in do_inhibit
inhibit_fd = await login_manager_interface.call_inhibit(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/mnt/storage/Programming/sparvio_toolbox/_host-env/lib64/python3.12/site-packages/dbus_fast/aio/proxy_object.py", line 92, in method_fn
msg = await self.bus.call(
^^^^^^^^^^^^^^^^^^^^
File "/mnt/storage/Programming/sparvio_toolbox/_host-env/lib64/python3.12/site-packages/dbus_fast/aio/message_bus.py", line 385, in call
await future
File "src/dbus_fast/aio/message_reader.py", line 19, in dbus_fast.aio.message_reader._message_reader
File "src/dbus_fast/_private/unmarshaller.py", line 775, in dbus_fast._private.unmarshaller.Unmarshaller._unmarshall
File "src/dbus_fast/_private/unmarshaller.py", line 636, in dbus_fast._private.unmarshaller.Unmarshaller._read_header
File "src/dbus_fast/_private/unmarshaller.py", line 376, in dbus_fast._private.unmarshaller.Unmarshaller._read_to_pos
File "src/dbus_fast/_private/unmarshaller.py", line 339, in dbus_fast._private.unmarshaller.Unmarshaller._read_sock_without_fds
EOFError
我不明白这是什么意思。我尝试查看systemd 中有关抑制锁的官方文档,但一无所获。如何使用 dbus_python 在 Python 中调用抑制锁Inhibit
的方法org.freedesktop.login1.Manager
并获取抑制锁?
由于某些原因,dbus-fast 默认不启用对文件描述符传输的支持。您必须将
negotiate_unix_fd=True
其指定为 MessageBus() 的参数才能启用此功能。