python - 如何在 Python 3 中将 asyncore 与多处理 IPC 模块一起使用?

标签 python sockets event-handling multiprocessing asyncore

我正在尝试使用 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/

相关文章:

python - unicode字符串格式

android - 将 grep 与 python subprocess.check_output 和 adb logcat 结合使用

python - gitpython中clone_from中的非值参数

将 uint16_t 转换为 char[2] 以通过套接字发送 (unix)

c++ - MS Visual C++ 通过单击按钮启动功能而不阻塞对话框

android - 在 EditText 中处理 “Enter” 并在其中插入字符

python - 为什么 `function` 不是 Python 中的关键字?

javascript - NodeJS 到客户端 : large data sending performance

c - 无法使用 tcpdump 捕获 IP 广播数据包

javascript - 嵌套 HTML 时如何控制 jQuery 事件?