Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying asgi and wsgi on Heroku

I'm trying to deploy Django Channels on Heroku using asgi alongside my existing wsgi implementation. Can I deploy both asgi and wsgi to heroku with the following setup?

My procfile:

web: gunicorn chatbot.wsgi --preload --log-file -
daphne: daphne chat.asgi:channel_layer --port $PORT --bind 0.0.0.0 -v2
chatworker: python manage.py runworker --settings=chat.settings -v2

My asgi.py file:

import os
from channels.asgi import get_channel_layer

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "chat.settings")

channel_layer = get_channel_layer()

My wsgi.py file:

import os

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "chat.settings")

application = get_wsgi_application()
application = DjangoWhiteNoise(application)

And my channel layers in settings.py:

CHANNEL_LAYERS = {
    'default': {
        "BACKEND": "asgi_redis.RedisChannelLayer",
        "CONFIG": {
            "hosts": [os.environ.get('REDIS_URL', 'redis://localhost:6379')]
        },
        'ROUTING': 'chat.routing.channel_routing',
    }
}
like image 890
user2155400 Avatar asked May 02 '17 15:05

user2155400


People also ask

What is ASGI vs Wsgi?

Like WSGI, ASGI describes a common interface between a Python web application and the web server. Unlike WSGI, ASGI allows multiple, asynchronous events per application. Plus, ASGI supports both sync and async apps.

Is Django ASGI or Wsgi?

As well as WSGI, Django also supports deploying on ASGI, the emerging Python standard for asynchronous web servers and applications.

Can Daphne run WSGI?

Running ASGI alongside WSGI If that's the case, that's fine; you can run Daphne and a WSGI server alongside each other, and only have Daphne serve the requests you need it to (usually WebSocket and long-poll HTTP requests, as these do not fit into the WSGI model).

How do I deploy a Python code on Heroku?

Uploading the Script Open the file using a text editor and add any dependencies needed such as numpy in order to run your project as when you deploy to Heroku the “pip” install command will be running to make sure all dependencies are present in order to run the script. 3. git add .


1 Answers

Figured this out, in case this might be relevant to anyone else. Using just asgi was the best solution. My procfile ended being:

web: daphne chat.asgi:channel_layer --port $PORT --bind 0.0.0.0 -v2
chatworker: python manage.py runworker --settings=chat.settings -v2

As a solution for serving static files, I updated my routing.py file to include a StaticFileConsumer.

like image 58
user2155400 Avatar answered Sep 22 '22 01:09

user2155400