• This repository has been archived on 13/Dec/2020
  • Stars
    star
    151
  • Rank 244,546 (Top 5 %)
  • Language
    Python
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Celery support for Flask without breaking PyCharm inspections.

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 2.6, 2.7, PyPy, 3.3, and 3.4 supported on Linux and OS X.
  • Python 2.7, 3.3, and 3.4 supported on Windows (both 32 and 64 bit versions of Python).
Build Status Windows Build Status Coverage Status Latest Version

Attribution

Single instance decorator inspired by Ryan Roemer.

Supported Libraries

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'
celery = Celery(app)

@celery.task()
def add_together(a, b):
    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():
    app = Flask(__name__)
    app.config['CELERY_IMPORTS'] = ('tasks.add_together', )
    app.config['CELERY_BROKER_URL'] = 'redis://localhost'
    app.config['CELERY_RESULT_BACKEND'] = 'redis://localhost'
    celery.init_app(app)
    return app
# tasks.py
from extensions import celery

@celery.task()
def add_together(a, b):
    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'
celery = Celery(app)
Redis(app)

@celery.task(bind=True)
@single_instance
def sleep_one_second(a, b):
    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.

Changelog

This project adheres to Semantic Versioning.

Unreleased

Changed
  • Supporting Flask 0.12, switching from flask.ext.celery to flask_celery import recommendation.

1.1.0 - 2014-12-28

Added
  • Windows support.
  • single_instance supported on SQLite/MySQL/PostgreSQL in addition to Redis.
Changed
  • CELERY_RESULT_BACKEND no longer mandatory.
  • Breaking changes: flask.ext.celery.CELERY_LOCK moved to flask.ext.celery._LockManagerRedis.CELERY_LOCK.

1.0.0 - 2014-11-01

Added
  • Support for non-Redis backends.

0.2.2 - 2014-08-11

Added
  • Python 2.6 and 3.x support.

0.2.1 - 2014-06-18

Fixed
  • single_instance arguments with functools.

0.2.0 - 2014-06-18

Added
  • include_args argument to single_instance.

0.1.0 - 2014-06-01

  • Initial release.

More Repositories

1

terminaltables

Project no longer maintained.
Python
689
star
2

Flask-Large-Application-Example

This is how I structure my large Flask applications.
Python
517
star
3

colorclass

Colorful worry-free console applications for Linux, Mac OS X, and Windows.
Python
58
star
4

libnl

Pure Python port of the Netlink protocol library suite.
Python
50
star
5

makemkv

Automated headless DVD/Bluray backups with MakeMKV from Docker.
Python
28
star
6

robpol86.com

My personal website.
Python
16
star
7

etaprogress

Easy to use ETA calculation and progress bar library.
Python
14
star
8

sphinx-disqus

Embed Disqus comments in Sphinx documents/pages.
Python
11
star
9

appveyor-artifacts

Download artifacts from AppVeyor builds of the same commit/pull request.
Python
10
star
10

logrus-custom-formatter

Customizable Logrus formatter similar in style to Python's logging.Formatter.
Go
10
star
11

influxdb

Docker-compose stack for InfluxDB, Grafana, and Cronitor
Shell
8
star
12

UnofficialDDNS

An unofficial dynamic DNS (DDNS) service for Name.com domains.
C#
8
star
13

Flask-Redis-Helper

Redis support for Flask without breaking PyCharm inspections.
Python
6
star
14

docoptcfg

docopt wrapper adding config file and environment variable support.
Python
5
star
15

flake8-pydocstyle

Abandoned
Python
5
star
16

Flask-Large-App-Example-2

Another example of a large Flask application.
JavaScript
4
star
17

EImage

Display images from image hosting services as if they were stored locally.
PHP
3
star
18

wifipy

Pure Python library for WiFi adapters. Communicates with the driver/kernel/OS directly.
Python
3
star
19

UnofficialDDNSnix

An unofficial dynamic DNS (DDNS) service for Name.com domains.
Python
3
star
20

Flask-JIRA-Helper

JIRA support for Flask without breaking PyCharm inspections.
Python
3
star
21

tape_bulk_eject

Eject multiple tapes from a Dell PowerVault 124T autoloader in one go.
Python
2
star
22

PythonTemplate

This is the template I use for my Python projects.
Python
2
star
23

robutils

Convenience classes for CLI Python applications.
Python
2
star
24

sphinx-imgur

Embed Imgur images and albums in Sphinx documents/pages.
Python
2
star
25

Flask-Statics-Helper

Provides Bootstrap3 and other static resources in a modular fashion.
JavaScript
2
star
26

general

General/misc scripts I use occasionally.
Python
1
star
27

qnx-neutrino-hello-world

Hello world for my MIB2 VW Infotainment System
1
star
28

jira-context

Cache JIRA basic authentication sessions to disk for console apps.
Python
1
star
29

gha_test

Test github actions
Makefile
1
star
30

GolangTemplate

This is the template I use for my Golang projects.
Makefile
1
star
31

githubBackup

Backups all of your GitHub public/private repos (with issues/wikis) and Gists.
Go
1
star
32

sphinx-carousel

A Sphinx extension for creating slideshows using Bootstrap Carousels.
Python
1
star