[英]How can I use asyncore with the multiprocessing IPC module in Python 3?
我正在嘗試利用multiprocessing
模塊的客戶端/服務器體系結構來促進我的Python腳本之間的通信。 我希望能夠使用asyncore
實現與主事件循環不同的單獨的連接偵聽器。 我怎樣才能做到這一點?
盡管沒有有關此操作的外部文檔,但我嘗試使用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
。
我認為您正在尋找的方法是handle_accept,而不是handle_accepted。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.