2012-09-19 39 views
0

我有一個用python編寫的LaunchDaemon,它使用root權限執行一個耗時長且資源密集的後臺任務。當它工作時,它需要發送狀態更新到多個GUI代理(每個登錄用戶一個),並且我努力尋找一個合適的IPC機制。由於守護進程和代理運行在不同的引導程序上下文中,共享對象將不起作用。我不能讓守護進程打開套接字,因爲它無法長時間回覆,客戶端也不得不輪詢更新。我所想到的最好的做法是讓每個代理打開自己的套接字和守護進程,然後依次寫入每個套接字,但是這看起來似乎不夠優雅。還有其他選擇嗎?哦,它必須在Mac OS X 10.5及更高版本上運行。LaunchDaemon IPC通知10.5+以上的多個GUI代理

回答

0

我結束了具有各GUI劑創建/ tmp下一個數據報套接字,然後launchdaemon反過來將消息發送到每一個和清理陳舊插口:

SOCKET_DIR = "/tmp" 
SOCKET_NAME = "se.gu.it.dafgu_migration_status" 

status_socket = None 

def set_status_menu(status, message): 
    global status_socket 
    try: 
     if status_socket is None: 
      status_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) 
     if status_socket is not None: 
      message = serializePlist({ 
       "DAFGUMigrationStatus": status, 
       "DAFGUMigrationMessage": message, 
      }) 
      for item in os.listdir(SOCKET_DIR): 
       if item.startswith(SOCKET_NAME): 
        socket_path = os.path.join(SOCKET_DIR, item) 
        logging.debug("Sending message to %s" % socket_path) 
        try: 
         status_socket.sendto(message, socket_path) 
        except socket.error, e: 
         if e[0] == errno.ECONNREFUSED: 
          logging.info("Removing stale socket %s" % socket_path) 
          os.unlink(socket_path) 
         else: 
          logging.warn("Sending to %s failed: %s" % (socket_path, e)) 
    except BaseException, e: 
     logging.debug("Unhandled exception when updating status menu: %s" % e) 

充分執行,可以發現在github上: