Alternative to gunicorn for an asgi django app
Hey everyone, I am attempting to host a personal project I have been working on to learn Django/Websockets. I am using Django Channels. I have run into quite a few issues with hosting the app on railway and currently it seems to be that I am trying to host an asgi application where as most of the hosting tutorials I have looked at have been using a wsgi application. Does anyone have any advice for hosting an asgi application and what to use instead of gunicorn? I am a bit confused as to what gunicorn actually does to be honest.
Any help or info would be much appreciated 🙂
25 Replies
Project ID:
7c4860ab-7860-4ab6-bbee-a6b54838c725
7c4860ab-7860-4ab6-bbee-a6b54838c725
you'll likely want daphne for django
Gunicorn is a production capable server for python. That’s why it’s used
You can use daphne like Brody said as well
and I was even gonna tag you in lol
Hahaha, I just happened to be on my phone when the post came through
You can look at the Django template to see how things are configured also
https://github.com/railwayapp-templates/django
GitHub
GitHub - railwayapp-templates/django: 1-Click Django on Railway
1-Click Django on Railway. Contribute to railwayapp-templates/django development by creating an account on GitHub.
I am using daphne actually, I followed the django channels tutorial from here (https://channels.readthedocs.io/en/stable/tutorial/part_1.html). I changed my railway.json to include this line:
"python manage.py migrate && python manage.py && daphne backend.asgi" instead and I will see how that goes.
thanks for the quick replies 🙂
Awesome! Report back for sure
I have been looking back and forth at the template and it's been super helpful, just struggling a bit to figure out what to change for asgi vs wsgi
Daphne may or may not need to be told to listen on host 0.0.0.0 and PORT
Really, you should only have to change which you are pointing to depending on the server you use
yes it does need to be told
use this instead
daphne -b 0.0.0.0 -p $PORT backend.asgi
I got this error from my last fix:
####
Traceback (most recent call last):
File "/opt/venv/bin/daphne", line 8, in <module>
sys.exit(CommandLineInterface.entrypoint())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/venv/lib/python3.11/site-packages/daphne/cli.py", line 171, in entrypoint
cls().run(sys.argv[1:])
File "/opt/venv/lib/python3.11/site-packages/daphne/cli.py", line 233, in run
application = import_by_path(args.application)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/venv/lib/python3.11/site-packages/daphne/utils.py", line 11, in import_by_path
module_path, object_path = path.split(":", 1)
^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: not enough values to unpack (expected 2, got 1)
####
not sure if it has anything to do with your suggestion but will try to add that in
Solution
actually I think this is the correct syntax
Yep that seems to be correct! Got rid of the previous errors and seems to be mostly working now, just a new error where I maybe have to install another dependency or something
do you have an endpoint for the root registered?
Not 100% sure I understand, so I assume not
The build was successful and I can connect to the server if thats what you mean. The link is
https://wordle-with-friends-backend-production.up.railway.app/
okay then all is good
looks like it works for me, at least the /games endpoint fetch request does
okay amazing! there seems to be an issue with connecting to the admin site but I think I just have to add something like:
CSRF_TRUSTED_ORIGINS = ['https://your-domain.com']
to my settings.py
I forget if you include the
https://
part, so read the docs on that, but yeah you got the right ideaIt worked with the https:// included!
Okay looks like I made a lot of good progress there! Thanks again for your help 🙂
no problem!
Awesome