How to run Gunicorn while still using WebSocket
Asked Answered
I

2

9

So I am using a docker for this python chat app project.

I originally had python manage.py runserver 0.0.0.0:8000 as my command in docker-compose.

I found that I should switch to gunicorn if I want to deploy my app on web (like heroku). The tutorial I found say simply change the command in docker-compose to gunicorn myproject.wsgi -b 0.0.0.0:8000. I did that, and all the websocket connections broke. There's a fail to send due to websocket is still in CONNECTING state, then after a while handshake fails with a status code of 404. All the setup were the same as before, except that one line. Just wondering what else I need to change to make websocket work with gunicorn? Thanks

EDIT: after some digging on the internet it seems that gunicorn wasn't supposed to be run with websocket (wsgi asgi difference I suppose?) If anyone could tell me something I could use instead of gunicorn for web server it would be extremely appreciated, or if there's any way I can run gunicorn with my django channels still working? Thanks!!

Infantine answered 30/1, 2020 at 2:7 Comment(0)
E
13

When using ASGI, for asynchronous servers (websockets), you should use an asynchronous server, like Daphne or Uvicorn, the Django documentation has examples on how to deploy for both of them.

If you want to use uvicorn directly you could do something like:

uvicorn myproject.asgi:application --host 0.0.0.0 --port 8000

You can also run uvicorn through gunicorn using the worker class:

gunicorn myproject.asgi:application -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000
Eade answered 30/1, 2020 at 6:55 Comment(0)
R
0

If you are using gunicorn with uvicorn worker and socker.io, try this

gunicorn myproject.asgi:application -w 1 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000

-w 1 Limiting gunicorn to 1 worker worked for me.

Resentment answered 11/4 at 10:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.