Flask-Worker simplifies interaction with a Redis Queue for executing long-running tasks in a Flask application.
Project description
Flask-Worker simplifies interaction with a Redis Queue for executing long-running tasks in a Flask application.
Long-running tasks are managed by a Worker, who sends the client a loading page until it completes the task. Upon completing the task, the Worker automatically replaces the client's window with the loaded page.
Why Flask-Worker
Suppose we have a view function which needs to execute a complex task before the client can view the page. What we want is for the complex task to run once, and for the view function to return a loading page while the complex task is running. Our first pass might be:
@app.route('/')
def index():
return complex_task()
Unfortunately, the view function executes the complex task every time it is called. To make matters worse, the client has no indication that the complex task is in progress. Each time the client tries to refresh the page, the complex task is queued up again.
Flask-Worker solves this problem. We set up a Worker:
class Worker(WorkerMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
def __init__(self, name):
self.name = name
self.set(complex_task)
super().__init__()
And call it in a view function:
@app.route('/')
def index():
worker = get_model(Worker, name='index')
return worker.result if worker.job_finished else worker()
See the tutorial for a complete example.
Installation
$ pip install flask-worker
Citation
@software{bowen2020flask-worker,
author = {Dillon Bowen},
title = {Flask-Worker},
url = {https://dsbowen.github.io/flask-worker/},
date = {2020-06-15},
}
License
Users must cite this package in any publications which use it.
It is licensed with the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for flask_worker-0.0.14-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d83ede525d0fa5521df33fd05e00b68a87264226fe7c9ee0f30178caba4bc5f |
|
MD5 | 5cc461575752fcd4e94a8e3f7fa62bce |
|
BLAKE2b-256 | 75afc1118fb26d37784b8d061e734bf3003ca7a13b17ddcd7764f7ec423b0829 |