Skip to main content

Simple Celery integration for Flask applications.

Project description

Build status Code coverage Maintenance yes GitHub license Documentation Status

Flask-Execute

Flask-Execute is a plugin for simplifying the configuration and management of Celery alongside a Flask application. It also slightly changes the paradigm for registering and dispatching celery tasks, exposing an API similar to the concurrent.futures API for submitting tasks to a separate executor.

Other features of the plugin include:

  • Automatic spin-up of local workers, queues, schedulers, and monitoring tools via configuration.

  • Automatic application context wrapping for celery workers.

  • Simpler API for submitting tasks to workers that doesn’t require pre-registration of tasks.

  • Result object API similar to concurrent.futures.Future API.

  • Flask CLI wrapper around the celery command that automatically wraps celery commands with an application context.

Installation

To install the latest stable release via pip, run:

$ pip install Flask-Execute

Alternatively with easy_install, run:

$ easy_install Flask-Execute

To install the bleeding-edge version of the project (not recommended):

$ git clone http://github.com/bprinty/Flask-Execute.git
$ cd Flask-Execute
$ python setup.py install

Usage

To set up an application with the extension, you can register the application directly:

from flask import Flask
from flask_execute import Celery

app = Flask(__name__)
plugin = Celery(app)

Or, via factory pattern:

celery = Celery()
app = Flask(__name__)
celery.init_app(app)

Once the plugin has been registered, you can submit a task using:

def add(x, y):
  return x + y

future = celery.submit(add, 1, 2)

# wait for result (not required)
future.result(timeout=1)

# cancel result
future.cancel()

# add callback function
def callback():
  # do something ...
  return

future.add_done_callback(callback)

Note that this plugin does not require users to pre-register tasks via the @celery.task decorator. This enables developers to more easily control whether or not task execution happens within the current session or on a separate worker. It also makes the API similar to the API provided by Dask and concurrent.futures. Also note that the celery command-line tool for spinning up local workers is no longer necessary. If no workers are connected, this plugin will automatically spin them up the first time a celery.submit() call is made.

Once a task as been submitted, you can monitor the state via:

task_id = future.id

# later in code

future = celery.get(task_id)
print(future.state)

You can also manage state updates within tasks with a more Flask-y syntax:

from flask_execute import current_task

def add(a, b):
  current_task.update_state(state='PROGRESS')
  return a + b

This plugin will also manage the process of spinning up local workers bound to your application the first time a celery.submit() call is made (if configured to do so). Additionally, the plugin will automatically wrap celery cli calls with your flask application (using the factory method or not), so you can more easily interact with celery:

# start local celery cluster with workers, flower monitor, and celerybeat scheduler
~$ flask celery cluster

# start local worker
~$ flask celery worker

# check status of running workers
~$ flask celery status

# shutdown all celery workers
~$ flask celery control shutdown

# shutdown all celery workers
~$ flask celery control shutdown

If your application uses the factory pattern with a create_app function for registering blueprints and plugin, you can use the standard flask cli syntax for automatically wrapping celery commands with your application context:

# check status of running workers
~$ FLASK_APP=app:create_app flask celery status

For more in-depth discussion on design considerations and how to fully utilize the plugin, see the User Guide.

Documentation

For more detailed documentation, see the Docs.

Questions/Feedback

File an issue in the GitHub issue tracker.

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

Flask-Execute-0.1.1.tar.gz (337.4 kB view details)

Uploaded Source

Built Distribution

Flask_Execute-0.1.1-py2.py3-none-any.whl (19.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file Flask-Execute-0.1.1.tar.gz.

File metadata

  • Download URL: Flask-Execute-0.1.1.tar.gz
  • Upload date:
  • Size: 337.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.7.5

File hashes

Hashes for Flask-Execute-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a0d1b0b2ad57c5d23b43cda4dffc6373f9f47b08470cf0478b883532de15faad
MD5 9e2c9ac99691a5b43958c15a1633f555
BLAKE2b-256 e62aea2b475830d4a872dd7db42462b4a155a68fedadf8aa2000d423f013b99d

See more details on using hashes here.

File details

Details for the file Flask_Execute-0.1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: Flask_Execute-0.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 19.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.7.5

File hashes

Hashes for Flask_Execute-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c8b6564a15c20efbe00155bfb14fe21217936cb6ef66d1030807e007245b90ce
MD5 6529d158148cce022df84fe95d183035
BLAKE2b-256 053ec11951f9dae94630323043e7178a8e9cf376d943965cae75d05be0a71c45

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page