如何在Python中用epoll()替换select()?

2 投票
1 回答
1457 浏览
提问于 2025-06-08 05:51

我正在编写一个Python的WebSocket聊天服务器。我用select()函数做了一个可以工作的服务器,可以监听客户端的连接,但当我在Windows上连接超过512个客户端,或者在Linux上连接超过1024个客户端时,我的服务器就崩溃了。经过研究,我发现这是系统的限制,我需要使用poll()或epoll()来支持更多的连接。

这是我用select()写的代码的一部分,我需要用epoll()或poll()来重写:

from select import select
rList, wList, xList = select(listeners, writers, listeners, interval)
for ready in wList:
    function1()
for ready in rList:
    function2()
for failed in xList:
    function3()

我该如何用epoll()或poll()来实现相同的功能呢?这些函数还是需要被调用的。

相关问题:

  • 暂无相关问题
暂无标签

1 个回答

1

我觉得应该是

rList, wList, xList = select.select(listeners, writers, listeners, interval)

你可以把它改成

poll = select.poll()
poll.register(eachconnection)
poll.poll(5000)

撰写回答