使用烧瓶插座时工作器超时

时间:2017-08-04 08:42:13

标签: python flask websocket flask-sockets

我试图在现有的烧瓶缓冲应用程序中支持websocket,将gunicorn超时设置为20秒。我决定使用flask-socket扩展,但事情进展顺利,但是在20秒后,greentlet会超时。

我假设websocket连接被视为普通的http请求,因此它应该返回一个使用greenlet超时的响应。

我添加了以下代码,并将gunicorn worker_class更改为geventwebsocket.gunicorn.workers.GeventWebSocketWorker

sockets = Sockets(app)
@sockets.route('/log')
def test_connect(socket):
    while not socket.closed:
        import time
        time.sleep(2)
        socket.send("heart beating")

我该如何处理这种情况?谢谢你的任何建议。

添加输出:

  Traceback (most recent call last):
File "/Users/bayi/Documents/dev/python/venvs/modulepub/lib/python2.7/site-packages/gevent/pywsgi.py", line 508, in handle_one_response
  self.run_application()
File "/Users/bayi/Documents/dev/python/venvs/modulepub/lib/python2.7/site-packages/geventwebsocket/handler.py", line 75, in run_application
  self.run_websocket()
File "/Users/bayi/Documents/dev/python/venvs/modulepub/lib/python2.7/site-packages/geventwebsocket/handler.py", line 52, in run_websocket
  list(self.application(self.environ, lambda s, h, e=None: []))
File "/Users/bayi/Documents/dev/python/venvs/modulepub/lib/python2.7/site-packages/zeus_core/wsgi/wsgi.py", line 332, in __call__
  return self.app(environ, start_response)
File "/Users/bayi/Documents/dev/python/venvs/modulepub/lib/python2.7/site-packages/flask/app.py", line 1997, in __call__
  return self.wsgi_app(environ, start_response)
File "/Users/bayi/Documents/dev/python/venvs/modulepub/lib/python2.7/site-packages/flask_sockets.py", line 45, in __call__
  handler(environment, **values)
File "./modulepub/app.py", line 49, in test_connect
  time.sleep(2)
File "/Users/bayi/Documents/dev/python/venvs/modulepub/lib/python2.7/site-packages/gevent/hub.py", line 75, in sleep
  hub.wait(loop.timer(seconds, ref=ref))
File "/Users/bayi/Documents/dev/python/venvs/modulepub/lib/python2.7/site-packages/gevent/hub.py", line 341, in wait
  result = waiter.get()
File "/Users/bayi/Documents/dev/python/venvs/modulepub/lib/python2.7/site-packages/gevent/hub.py", line 568, in get
  return self.hub.switch()
File "/Users/bayi/Documents/dev/python/venvs/modulepub/lib/python2.7/site-packages/gevent/hub.py", line 331, in switch
  return greenlet.switch(self)
Timeout: 20 seconds

1 个答案:

答案 0 :(得分:0)

发现超时是由我们使用的框架设置的。它为烧瓶app添加了一个中间件来为每个api调用设置超时。更改超时配置可以解决这个问题。

相关问题