This repository demonstrates use of Redis and RQ for asynchronously executing long running tasks in Plotly Dash. The task the app executes is meaningless, it converts a string to upper case character by character with a time delay for each character. Hopefully however the programming pattern is clear and the example should be easily adaptable to other applications.
From the RQ docs:
RQ (Redis Queue) is a simple Python library for queueing jobs and processing them in the background with workers.
This example uses a callback to add longer running tasks to an RQ job queue. A second callback firing on an interval checks the current status of the job, either retrieving the result or updating a progress bar to indicate progress made on the task.
This example can be run locally, or deployed as is to Heroku.
Start by cloning this repository to your machine.
git clone https://github.com/tcbegley/dash-rq-demo.git
cd dash-rq-demo
If you have Docker installed, run the app with
docker compose up
The app can be accessed at localhost:8050.
You can alternatively run docker-compose.dev.yml
for development purposes.
This volume mounts the source code into the container and uses a development
server so that you can benefit from hot-reloading without rebuilding the
container.
docker compose -f docker-compose.dev.yml up
If you don't want to use Docker, first make sure you have Python>=3.7 and Redis installed. Once you've done this you will need to start a Redis server. See the links for more details, but probably you will want to run something like:
redis-server &
Then do the following (preferably in a virtual environment):
pip install -r requirements.txt
# runs worker.py in the background and run_locally.py
python worker.py & python run_locally.py
The app can be accessed at localhost:8050.
To deploy your own copy of this app on Heroku, just click on this button:
Note: you may need to wait a few minutes for the Redis addon to start before the app starts working.
Alternatively if you would like to set things up manually, follow the below steps. You will need to install the Heroku CLI.
First clone this repository and navigate to it
git clone https://github.com/tcbegley/dash-rq-demo.git
cd dash-rq-demo
Create a new Heroku app and push the contents of this repository
heroku create
git push heroku main
Create the Redis addon, note that this can take a few minutes to start.
heroku addons:create heroku-redis:hobby-dev
You can check the status of the addon with the following command. The app will not work until the addon has been successfully created.
heroku addons:info heroku-redis
Add a worker to handle the background tasks.
heroku scale worker=1
Open the deployed app in your browser
heroku open
If something is unclear or you find a bug feel free to submit an issue or pull request.