uWSGI原生异步WebSocket与Redis的坏文件描述符
你好,我有一个简单的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()
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]
我知道我在一个无限循环中发送最后一条消息,但这样做是为了测试系统的极限。我想知道失败的原因是什么,以及有没有什么办法可以让系统在失败之前发送更多的消息。
我在Ubuntu 12.04上运行这段代码,使用的命令是:
uwsgi --http :8080 --http-websockets --async=1000 --ugreen --wsgi-file SocketServer.py运行这个示例的异步聊天客户端应用程序 https://github.com/unbit/uwsgi/blob/master/tests/websockets_chat_async.py 在用户负载很大的情况下也会出现相同的错误。
1 个回答
1
Strace和Lsof是你在调试时的好帮手。Strace可以告诉你正在运行的程序都调用了哪些系统功能。你可以用“strace -p ”这个命令把它连接到正在运行的程序上,这样就能看到它调用的系统功能列表。然后你可以尝试重现你遇到的问题。希望你能在这些事件中找到一些有用的信息。Lsof则可以帮你查看文件描述符和实际文件之间的关系,就像给你一张文件的地图。