bayi
bayi

Reputation: 21

Worker timeout when using flask-sockets

I'm trying to support websocket in a existing flask restful app which set the gunicorn timeout to 20 seconds. I decided to use flask-sockets extension and things goes well except the greentlet would timeout after 20 seconds.

I assume that the websocket connection is treated as a normal http request so it's expected to return a response util the greenlet timeout.

I added the following code and changed the gunicorn worker_class to 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")

How should I handle this situation? Thanks for any advice.

add output:

  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

Upvotes: 1

Views: 932

Answers (1)

bayi
bayi

Reputation: 21

Found that the timeout is set by the framework we use. It add a middleware to flask app to set timeout for each api call.Changing the timeout config can solve this.

Upvotes: 1

Related Questions