【发布时间】:2017-04-04 03:10:27
【问题描述】:
我有以下简化的代码。它监听 D-Bus 并在创建新作业时执行某些操作。为此,我需要启动 GLib.MainLoop().run(),因为我发现了多个示例。
在执行此操作时,我希望程序持续侦听 IPC 总线并在收到消息时执行某些操作。但显然这不起作用,因为我的程序卡在GLib.MainLoop().run()。
如何实现让我同时听 D-Bus 和 IPC 的东西?
#!/usr/bin/env python3.4
import asgi_ipc as asgi
from gi.repository import GLib
from pydbus import SystemBus
from systemd.daemon import notify as sd_notify
def main():
bus = SystemBus()
systemd = bus.get(".systemd1")
systemd.onJobNew = do_something_with_job()
channel_layer = asgi.IPCChannelLayer(prefix="endercp")
# Notify systemd this unit is ready
sd_notify("READY=1")
GLib.MainLoop().run()
while True:
message = channel_layer.receive(["endercp"])
if message is not (None, None):
do_something_with_message(message)
if __name__ == "__main__":
# Notify systemd this unit is starting
sd_notify("STARTING=1")
main()
# Notify systemd this unit is stopping
sd_notify("STOPPING=1")
【问题讨论】:
标签: python-3.x python-3.4 glib dbus systemd