2013-03-18 123 views
2

我試圖利用multiprocessing模塊的客戶/服務器體系結構來促進我的Python腳本之間的通信。我希望能夠使用asyncore來實現一個獨立的連接監聽器,除了我的主事件循環。我怎樣才能做到這一點?如何在Python 3中使用asyncore與多處理IPC模塊?

雖然是如何這樣做沒有外在的文檔,我試着用監聽套接字從multiprocessing.Listener構建基本asyncore.dispatcher

import asyncore 
import socket 
from multiprocessing.connection import Listener, Client 

class ConnectionListener(asyncore.dispatcher): 
    def __init__(self, ln): 
     asyncore.dispatcher.__init__(self) 
     self.set_socket(ln._listener._socket) 
    def handle_accepted(self, sock, addr): 
     print("Accepted a client!") 

if __name__ == "__main__": 
    address = ('localhost', 19378) 
    try: 
     ln = Listener(address) 
     x = ConnectionListener(ln) 
     while True: 
      asyncore.loop() 
    except socket.error: 
     c = Client(address) 
     print("Client is trying to connect") 

當服務器執行循環,它永遠不會觸發handle_accepted

回答

0

我認爲你正在尋找的方法是handle_accept,而不是handle_accepted。

相關問題