Johnston
Johnston

Reputation: 20894

How to run python websocket on gunicorn

I found this 0 dependency python websocket server from SO:
https://gist.github.com/jkp/3136208
I am using gunicorn for my flask app and I wanted to run this websocket server using gunicorn also. In the last few lines of the code it runs the server with:

if __name__ == "__main__":
    server = SocketServer.TCPServer(
        ("localhost", 9999), WebSocketsHandler)
    server.serve_forever()

I cannot figure out how to get this websocketserver.py running in gunicorn. This is because one would think you would want gunicorn to run server_forever() as well as the SocketServer.TCPServer(.... Is this possible?

Upvotes: 3

Views: 11453

Answers (2)

Depado
Depado

Reputation: 4929

If you use Flask-Sockets extension, you have a websocket implementation for gunicorn directly in the extension which make it possible to start with the following command line :

gunicorn -k flask_sockets.worker app:app

Though I don't know if that's what you want to do.

Upvotes: 4

Cilyan
Cilyan

Reputation: 8491

GUnicorn expects a WSGI application (PEP 333) not just a function. Your app has to accept an environ variable and a start_response callback and return an iterator of data (roughly speaking). All the machinery encapsuled by SocketServer.StreamRequestHandler is on gunicorn side. I imagine this is a lot of work to modify this gist to become a WSGI application (But that'll be fun!).

OR, maybe this library will get the job done for you: https://github.com/CMGS/gunicorn-websocket

Upvotes: 5

Related Questions