简体   繁体   English

如何在Python 3中的异步处理IPC模块中使用asyncore?

[英]How can I use asyncore with the multiprocessing IPC module in Python 3?

I'm trying to utilise the Client/Server architecture of the multiprocessing module to facilitate communication between my Python scripts. 我正在尝试利用multiprocessing模块的客户端/服务器体系结构来促进我的Python脚本之间的通信。 I want to be able to implement a separate connection listener apart from my main event loop, using asyncore . 我希望能够使用asyncore实现与主事件循环不同的单独的连接侦听器。 How can I do this? 我怎样才能做到这一点?

While there is no outward documentation of how to do so, I've tried to construct a basic asyncore.dispatcher using the listener socket from multiprocessing.Listener : 尽管没有有关此操作的外部文档,但我尝试使用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")

While the server does loop, it never fires handle_accepted . 服务器确实循环时,从不触发handle_accepted

我认为您正在寻找的方法是handle_accept,而不是handle_accepted。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM