- Our app without channels
- Setting up Channels
- Adding routes to routing.py
- Modifying our view
- Install & turn on redis
- Activate Consumer
- Pushing Project to Heroku
Our app without channels
I have put together a bear bones app that will help me demenstrate how performing background task can inhance what we are trying to do.
What our app does:
Our app currently has one core function. A Customer Model that updates its status to true or false depending on its previous status. If the current status is true, then the next time the method is called it will set it to false, Kinda pointless i know, but it will help demonstrate what im trying to do.
Our view will activate this method for every model. When we open up our home page our view will loop through and call the updateCustomer() method.
I have added three customers. Here is our desired outcome in the command prompt each time we open our page.
I added a delay to show later how a worker can save us from timing out our page & crashing our app.
Setting up channels
Now lets configure channels so we can offload this task and have it work in the background while without interrupting our site experience
We will need to install a few dependencies before we start.
- Pip install channels
- Add channels to INSTALLED_APPS in settings.py
- Add a CHANNEL_LAYERS & CACHES varible to our settings.py
First lets pip install channels:
pip install channels
In settings.py INSTALLED_APPS add ‘channels’:
No that we have this our app will have an error. This is because channels runs off of ASGI instead of WSGI.
This is the error you will get:
Add a CHANNEL_LAYERS & CACHES variable to our settings.py
We will need to add one more variable called WSGI_APPLICATION but first need to make a few more files before it can work.
Create a file called Routing.py in the same direcotry as settings.py
Once your routing.py file looks at a minimum like the one above, you can add this line of code under CACHES in settings.py.
You application should now be able to work and should read something like the image below:
A consumer is simply an object with asynchronous capabilities and methods that lives in consumers.py, wich should live in your app (not the root folder).
Adding routs to routing.py
Now that we have built our consumer, we need to add a route to our consumer in a simular way we add paths for urls that activate views.
Heres what our routing.py file should look like now:
Modifying our view
In our consumer we built a method called performUpdates. This method will be what we call to activate our task in our view.
In our view we made a few more imports, specified which method of our consumer we wanted to activate in the data dictionary and finally specified the route to our consumer from routing.py.
Install & Turn on redis server
Our redis server will be what handels the consumers tasks allowing them to be performed in the background. To install redis simply go to redis.io and follow the steps on how to download.
Once you have dwonloaded redis, open the redis folder and dowble click “Redis-server”.
If this command prompt opens then your server was installed correctly and is running.
You can dowble check if your server is running by opening up “redis-cli” from the same folder and typing “ping”. If its working properly it should give you a response of “pong”
Finally, to activate a consumer we need to open up another command prompt, file path your way into your project just as you would before you turn on your dajno server and run:
python manage.py runworker workerRoutName
If your worker is running properly this is what you should see.
Thats it! Now when you open up your page, All the outputs will be in the workers command prompt. If you open up your page and close it quickly, the worker will still run the task as long as your workers command prompt is open.Pushing Project to Heroku
Pushing Project to Heroku
When we push this project live there are additional steps we will need to take to ensure it works propely.
Heroku Redis Addon – Your heroku account under addons do a search redis. You will need to dwonload this addon so it works just like your local redis server.
You will need to create an asgi.py file in the same diorectory as your wsgi.py. Heroku needs to know wsgi.py was replaced and needs to find the contets of ASGI. This is what your folder should look like:
Our procfile is the final connecting piece. This file will tell heroku to add a background worker dyno & will also connect us to asgi.py
Last piece i almost forgot. The Channels interface server is called Daphne. Daphne can be run with a simple pip install.
pip install daphne
Once your done dont forget to run pip freeze to ensure all libraries were included.
For the production version we will just need to pip install channels_redis and add to our requirements.txt file.
- Coding For entrepreneurs: Chat x Channels
- Worker and Background Tasks
- Django Channels for Background Tasks
- Finally, Real-Time Django Is Here: Get Started with Django Channels