- Notifications
You must be signed in to change notification settings - Fork6
Long running tasks in Dash using RQ
License
tcbegley/dash-rq-demo
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
This repository demonstrates use of Redis and RQ for asynchronously executinglong running tasks in Plotly Dash. The task the app executes is meaningless, itconverts a string to upper case character by character with a time delay foreach character. Hopefully however the programming pattern is clear and theexample should be easily adaptable to other applications.
From theRQ docs:
RQ (Redis Queue) is a simple Python library for queueing jobs and processingthem in the background with workers.
This example uses a callback to add longer running tasks to an RQ job queue. Asecond callback firing on an interval checks the current status of the job,either retrieving the result or updating a progress bar to indicate progressmade on the task.
This example can be run locally, or deployed as is toHeroku.
Start by cloning this repository to your machine.
git clone https://github.com/tcbegley/dash-rq-demo.gitcd dash-rq-demo
If you haveDocker installed, run the app with
docker compose up
The app can be accessed atlocalhost:8050.
You can alternatively rundocker-compose.dev.yml
for development purposes.This volume mounts the source code into the container and uses a developmentserver so that you can benefit from hot-reloading without rebuilding thecontainer.
docker compose -f docker-compose.dev.yml up
If you don't want to use Docker, first make sure you havePython>=3.7 andRedis installed. Once you've done this youwill need tostart a Redis server. See the links for moredetails, 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.pypython worker.py& python run_locally.py
The app can be accessed atlocalhost: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 belowsteps. You will need to install theHeroku CLI.
First clone this repository and navigate to it
git clone https://github.com/tcbegley/dash-rq-demo.gitcd dash-rq-demo
Create a new Heroku app and push the contents of this repository
heroku creategit 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 pullrequest.
About
Long running tasks in Dash using RQ