Python uWSGI本机异步WebSocket和redis的错误文件描述符
嗨,我有一个简单的websocket服务器,它将消息推送到客户端,代码如下Python uWSGI本机异步WebSocket和redis的错误文件描述符,python,websocket,redis,uwsgi,asyncsocket,Python,Websocket,Redis,Uwsgi,Asyncsocket,嗨,我有一个简单的websocket服务器,它将消息推送到客户端,代码如下 uwsgi.websocket_handshake(env['HTTP_SEC_WEBSOCKET_KEY'], env.get('HTTP_ORIGIN', '')) print("websockets...") r = redis.StrictRedis(host='localhost', port=6379, db=0) channel = r.pubsub() channe
uwsgi.websocket_handshake(env['HTTP_SEC_WEBSOCKET_KEY'], env.get('HTTP_ORIGIN', ''))
print("websockets...")
r = redis.StrictRedis(host='localhost', port=6379, db=0)
channel = r.pubsub()
channel.subscribe('backchannel')
websocket_fd = uwsgi.connection_fd()
redis_fd = channel.connection._sock.fileno()
while True:
uwsgi.wait_fd_read(websocket_fd, 3)
uwsgi.wait_fd_read(redis_fd)
uwsgi.suspend()
fd = uwsgi.ready_fd()
if fd > -1:
if fd == websocket_fd:
msg = uwsgi.websocket_recv_nb()
if msg:
r.publish('backchannel', msg)
elif fd == redis_fd:
msg = channel.parse_response()
print(msg)
# only interested in user messages
t = 'message'
if sys.version_info[0] > 2:
t = b'message'
if msg[0] == t:
uwsgi.websocket_send("[%s] %s" % (time.time(), msg))
else:
# on timeout call websocket_recv_nb again to manage ping/pong
msg = uwsgi.websocket_recv_nb()
if msg:
r.publish('backchannel', msg)
r.publish('backchannel', "Resistence is Futile!")
执行此代码时,在推送大约500条消息后会导致以下错误
epoll_ctl(): Bad file descriptor [core/event.c line 520]
Traceback (most recent call last):
File "SocketServer.py", line 71, in application
uwsgi.wait_fd_read(redis_fd)
IOError: unable to fd 9 to the event queue
epoll_ctl(): Bad file descriptor [core/event.c line 635]
我知道我在无限循环中发送最后一条消息,但我这样做是为了测试系统的极限。我想知道的是失败的原因,如果我能做些什么让系统在失败之前推送更多消息
我在Ubuntu12.04上使用以下命令运行此代码
uwsgi--http:8080--HTTPWebSockets--async=1000--ugreen--wsgi文件SocketServer.py
运行示例异步聊天客户端应用程序
在用户负载较重的情况下运行时会导致相同的错误。Strace和Lsof是您的朋友。Strace将为您提供一个由正在运行的进程进行的系统调用的列表。使用“strace-p”将其附加到正在运行的进程,以查看系统调用列表,然后尝试重现这种情况。希望您能在发生的事件流中找到有用的东西。Lsof将为您提供一个文件描述符到实际文件的映射。谢谢sharjeel,我认为问题的根源在于上述代码中调用redis的方式。我只是不明白为什么它会失败。再说一遍,strace会很有帮助的。