2016-11-20 3 views
4

私は次のような単純なコードを持っています。それはDバスに耳を傾け、新しい仕事が生まれたときに何かをします。それがうまくいくには、私が見つけた複数の例で提示されているように、GLib.MainLoop().run()を開始する必要があります。DバスイベントとIPCチャネルを同時に聴くには?

これを実行している間、私はプログラムがIPCバスを継続的に聴き、メッセージを受信したときに何かをしたいと思っています。しかし、明らかに私のプログラムはGLib.MainLoop().run()に固執しているので動作しません。

Dバスと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") 

答えて

2

IPCChannelLayer.receive()はブロックされないため、アイドルコールバックで実行できます。試してみよう:

callback_id = GLib.idle_add(GLib.PRIORITY_DEFAULT_IDLE, poll_channel, data=channel_layer) 
GLib.MainLoop().run() 
GLib.idle_remove_by_data(channel_layer) 

# ... 

def poll_channel(channel_layer): 
    message = channel_layer.receive(["endercp"]) 
    if message is not (None, None): 
     do_something_with_message(message) 
    return GLib.SOURCE_CONTINUE 
関連する問題