IlanL
IlanL

Reputation: 65

Production Flask-SocketIO + ZeroRPC

I've deployed a flask-socketio web server, but after installing zerorpc which installs gevent i'm facing a lot of troubles.. at first my code looked like this:

socketio.start_background_task(poll_events)
socketio.run(app, host="0.0.0.0", keyfile='key.pem', certfile='cert.pem')

I'm starting a background task which will constantly read from a queue and send messages through socketio. now that gevent is installed it flask-socketio will try to use it (which i'm fine with actually making my server a production server and not a development one) but then socketio.start_background_task blocks. So I read that

from gevent import monkey; monkey.patch_all()

is required. So now my code looks like that:

socketio.start_background_task(poll_events)
WSGIServer(('0.0.0.0', 5000), app, keyfile='key.pem', certfile='cert.pem').serve_forever()

For some reason when debugging with pycharm I received a lot of weird greenlet exceptions and also I think that sometimes socketio messages are dropped so I decided to use eventlet. Then again, patching is required. So my code looks like this:

socketio.start_background_task(poll_events)
eventlet.wsgi.server(eventlet.wrap_ssl(eventlet.listen(("0.0.0.0", 5000)), keyfile='key.pem', certfile='cert.pem'), app)

Because of monkey patching zerorpc throws an exception "gevent.exceptions.LoopExit: This operation would block forever"

What is the correct way to deploy a production server with flask + socketio + zerorpc?

Upvotes: 1

Views: 227

Answers (1)

IlanL
IlanL

Reputation: 65

I've resolved the issue, when debugging I choose "threading" as async_mode

 socketio = SocketIO(app, async_mode="threading")

and when deploying with gunicorn I use gevent

CMD ["gunicorn", "-w", "1", "-k", "gevent","--reload", "web_app:app"]

For some reason gevent doesn't work without gunicorn and eventlet wouldn't work with zerorpc..

Upvotes: 0

Related Questions