1

我正在尝试在现有的烧瓶 restful 应用程序中支持 websocket,它将 gunicorn 超时设置为 20 秒。我决定使用flask-sockets扩展,一切顺利,除了greentlet会在20秒后超时。

我假设 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
4

1 回答 1

1

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

于 2017-08-08T06:20:54.537 回答