0

我目前正在研究一个允许在同一个监听套接字上进行多处理的 websocket 实现。我能够在四核机器上通过 4 个进程实现惊人的性能。

当我上层时,比如 8 个进程,在 4 个请求之后,epoll.poll 不再触发任何事件。有趣的是,我尝试运行相同的程序,在 2 个不同的端口上有 2 个侦听器。每个侦听器有 4 个进程,它在每个套接字 2 个请求后阻塞。每个侦听器有 2 个进程,我都可以顺利通过它。

任何想法?

.py(提取)

#create the WSServer
wsserver = WSServer(s.bind_ip, s.bind_port, s.max_connections)
# specify on how many process we'll run
wsserver.num_process = s.num_process
Process(target=wsserver.run,args=()).start()

wsserver.py(提取)

def serve_forever_epoll(wsserver):
    log(current_process())
    epoll = select.epoll()
    epoll.register(wsserver.socket.fileno(), select.EPOLLIN)
    try:
        client_map = {}
        while wsserver.run:
            events = epoll.poll(1)
            for fileno, event in events:
                if fileno == wsserver.socket.fileno():
                    channel, details = wsserver.socket.accept()
                    channel.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
                    aclient = wsclient.WSClient(channel, wsserver, process_server.client_manager)
                    client_map[channel.fileno()] = aclient
                    epoll.register(channel.fileno(), select.EPOLLIN )
                    log('Accepting client on %s' % current_process())
                    aclient.do_handshake()

                elif event & select.EPOLLIN:
                        aclient = client_map[fileno]
                        threading.Thread(target=aclient.interact).start()

    except Exception, e:
        log(e)
    finally:
        epoll.unregister(wsserver.socket.fileno())
        epoll.close()
        wsserver.socket.close()

class WSServer():

    def __init__(self, address, port, connections):
        self.address = address
        self.port = port
        self.connections = connections
        self.onopen = onopen
        self.onclose = onclose
        log('server init')
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        #self.socket.setblocking(0)
        self.socket.bind((self.address, int(self.port)))
        self.socket.listen(self.connections)

    def run(self, *args):
        multiprocessing.log_to_stderr(logging.DEBUG)
        log("Run server")
        try:

            log("Starting Server")
            self.run = True
            serve_forever = serve_forever_epoll
            for i in range(self.num_process-1):
                log('Starting Process')
                Process(target=serve_forever,args=(self,)).start()
            serve_forever(self)

        except Exception as e:
            log("Exception-- %s " % e)
            pass
4

1 回答 1

0

好的,所以最后这个奇怪的案例是由我正在使用的另一个模块引起的。我使用 Pyro4 作为管理器来跟踪哪个进程拥有哪个客户端。这大大简化了 IPC,也允许我基于一些 user_data 进行一些客户端过滤。

问题是 Pyro4 守护进程在 MainProcess 上运行,但不在 Main Thread 上运行!...只要我的进程少于 4 个,一切都很好(不要问我为什么)。在主进程 + 线程事件循环中移动 Pyro,它工作得很好!所以现在,我能够为同一个监听端口实现 8、16 或 32 个进程,以及生成新配置来复制它或为 websocket 服务器公开一个新端点!

感谢您的贡献,对不起您的时间...

于 2012-08-21T20:19:19.587 回答