加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 使用uWSGI本机异步websockets和redis的错误文件描述符

发布时间:2020-12-20 13:40:43 所属栏目:Python 来源:网络整理
导读:嗨我有一个简单的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.pubs
嗨我有一个简单的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
在大量用户负载下运行时会导致相同的错误.

解决方法

Strace和Lsof是你的朋友. Strace将为您提供正在运行的进程所进行的系统调用列表.使用“strace -p”将其附加到正在运行的进程以查看系统调用列表,然后尝试重现该情况.希望你能在发生的事件流中找到有用的东西. Lsof将为您提供实际文件的文件描述符映射.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读