我正在尝试使用 multiprocessing
的客户端/服务器架构。模块以促进我的 Python 脚本之间的通信。我希望能够使用 asyncore
在主事件循环之外实现单独的连接监听器.我怎样才能做到这一点?
虽然没有关于如何执行此操作的外部文档,但我尝试构建一个基本的 asyncore.dispatcher
使用来自 multiprocessing.Listener
的监听器套接字:
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。
关于python - 如何在 Python 3 中将 asyncore 与多处理 IPC 模块一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15474432/