Celery support for Flask without breaking PyCharm inspections.
Project description
Flask-Celery-Tools
This is a fork of Flask-Celery-Helper
Even though the Flask documentation says Celery extensions are
unnecessary now, I found that I still need an extension to properly use Celery in large Flask applications. Specifically
I need an init_app() method to initialize Celery after I instantiate it.
This extension also comes with a single_instance method.
- Python PyPy, 3.11, 3.12 and 3.13 supported.
Attribution
Single instance decorator inspired by Ryan Roemer.
Quickstart
Install:
pip install Flask-Celery-Helper
Examples
Basic Example
# example.py
from flask import Flask
from flask_celery import Celery
app = Flask('example')
app.config['CELERY_BROKER_URL'] = 'redis://localhost'
app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost'
app.config['CELERY_TASK_LOCK_BACKEND'] = 'redis://localhost'
celery = Celery(app)
@celery.task()
def add_together(a: int, b: int) -> int:
return a + b
if __name__ == '__main__':
result = add_together.delay(23, 42)
print(result.get())
Run these two commands in separate terminals:
celery -A example.celery worker
python example.py
Factory Example
# extensions.py
from flask_celery import Celery
celery = Celery()
# application.py
from flask import Flask
from extensions import celery
def create_app() -> Flask:
app = Flask(__name__)
app.config['CELERY_IMPORTS'] = ('tasks.add_together', )
app.config['CELERY_BROKER_URL'] = 'redis://localhost'
app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost'
app.config['CELERY_TASK_LOCK_BACKEND'] = 'redis://localhost'
celery.init_app(app)
return app
# tasks.py
from extensions import celery
@celery.task()
def add_together(a: int, b: int) -> int:
return a + b
# manage.py
from application import create_app
app = create_app()
app.run()
Single Instance Example
# example.py
import time
from flask import Flask
from flask_celery import Celery, single_instance
from flask_redis import Redis
app = Flask('example')
app.config['REDIS_URL'] = 'redis://localhost'
app.config['CELERY_BROKER_URL'] = 'redis://localhost'
app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost'
app.config['CELERY_TASK_LOCK_BACKEND'] = 'redis://localhost'
celery = Celery(app)
Redis(app)
@celery.task(bind=True)
@single_instance
def sleep_one_second(a: int, b: int) -> int:
time.sleep(1)
return a + b
if __name__ == '__main__':
task1 = sleep_one_second.delay(23, 42)
time.sleep(0.1)
task2 = sleep_one_second.delay(20, 40)
results1 = task1.get(propagate=False)
results2 = task2.get(propagate=False)
print(results1) # 65
if isinstance(results2, Exception) and str(results2) == 'Failed to acquire lock.':
print('Another instance is already running.')
else:
print(results2) # Should not happen.
Locking backends
Flask-Celery-Tools supports multiple locking backends you can use:
Filesystem
Filesystem locking backend is using file locks on filesystem where worker is running, WARNING this backend is not usable for distributed tasks!!!
Redis
Redis backend is using redis for storing task locks, this backend is good for distributed tasks.
Database (MariaDB, PostgreSQL, etc)
Database backend is using database supported by SqlAlchemy to store task locks, this backend is good for distributed tasks. Except sqlite database that have same limitations as filesystem backend.
Project details
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flask_celery_tools-1.5.3.tar.gz.
File metadata
- Download URL: flask_celery_tools-1.5.3.tar.gz
- Upload date:
- Size: 13.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd5719442c0314cbaf08b90f610bd1c8a06bdea779a1812d79aeff9ee31ce52a
|
|
| MD5 |
417aa23d6c585ddd9da0e8c32346bd51
|
|
| BLAKE2b-256 |
5f0da209098891e3efa5313c50c29fcf20a41952710c0cc60975c49aeafeed24
|
File details
Details for the file flask_celery_tools-1.5.3-py3-none-any.whl.
File metadata
- Download URL: flask_celery_tools-1.5.3-py3-none-any.whl
- Upload date:
- Size: 13.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b4db7978b4f08c1740776043b022c5ba70dae81dab64316cf56e95303af10769
|
|
| MD5 |
51201d91c3ec0f5168e7526a764f58c6
|
|
| BLAKE2b-256 |
b043dde33f6a68468569a0266f503f950f0b4cd9dc1d07d916dd4eeff27c02d6
|