Skip to main content

An easy to use Flask wrapper for concurrent.futures

Project description

Flask-Executor

Build Status Coverage Status PyPI Version GitHub license

Sometimes you need a simple task queue without the overhead of separate worker processes or powerful-but-complex libraries beyond your requirements. Flask-Executor is an easy to use wrapper for the concurrent.futures module that lets you initialise and configure executors via common Flask application patterns. It's a great way to get up and running fast with a lightweight in-process task queue.

Installation

Flask-Executor is available on PyPI and can be installed with:

pip install flask-executor

Quick start

Here's a quick example of using Flask-Executor inside your Flask application:

from flask import Flask
from flask_executor import Executor

app = Flask(__name__)

executor = Executor(app)


def send_email(recipient, subject, body):
    # Magic to send an email
    return True


@app.route('/signup')
def signup():
    # Do signup form
    executor.submit(send_email, recipient, subject, body)

Contexts

When calling submit() or map() Flask-Executor will wrap ThreadPoolExecutor callables with a copy of both the current application context and current request context. Code that must be run in these contexts or that depends on information or configuration stored in flask.current_app, flask.request or flask.g can be submitted to the executor without modification.

Futures

You may want to preserve access to Futures returned from the executor, so that you can retrieve the results in a different part of your application. Flask-Executor allows Futures to be stored within the executor itself and provides methods for querying and returning them in different parts of your app::

@app.route('/start-task')
def start_task():
    executor.submit_stored('calc_power', pow, 323, 1235)
    return jsonify({'result':'success'})

@app.route('/get-result')
def get_result():
    if not executor.futures.done('calc_power'):
        return jsonify({'status': executor.futures._state('calc_power')})
    future = executor.futures.pop('calc_power')
    return jsonify({'status': done, 'result': future.result()})

Decoration

Flask-Executor lets you decorate methods in the same style as distributed task queues like Celery:

@executor.job
def fib(n):
    if n <= 2:
        return 1
    else:
        return fib(n-1) + fib(n-2)

@app.route('/decorate_fib')
def decorate_fib():
    fib.submit(5)
    fib.submit_stored('fibonacci', 5)
    fib.map(range(1, 6))
    return 'OK'

Documentation

Check out the full documentation at flask-executor.readthedocs.io!

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-Executor-0.8.2.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Flask_Executor-0.8.2-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file Flask-Executor-0.8.2.tar.gz.

File metadata

  • Download URL: Flask-Executor-0.8.2.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for Flask-Executor-0.8.2.tar.gz
Algorithm Hash digest
SHA256 a62ccad272c8cd912cd7f02b5bff1f4956277c5f09dc99c45d546f3e631ec5bc
MD5 3d84cac9fa5611e23e4e145d30b95f04
BLAKE2b-256 6c9babcd0334f09ba34d3f6d1bf9085429dad428628138dba37f04b8cefb0662

See more details on using hashes here.

File details

Details for the file Flask_Executor-0.8.2-py3-none-any.whl.

File metadata

  • Download URL: Flask_Executor-0.8.2-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

File hashes

Hashes for Flask_Executor-0.8.2-py3-none-any.whl
Algorithm Hash digest
SHA256 40899cf0df91bec2a9205080ea7f106c27af8165437dbdcea5b07faceeb5e5b1
MD5 a0f919341ef91e8b173112aed6774d2c
BLAKE2b-256 93694c714447e292155cabc95952fb9ac09ae6f296ac300d2d40f301a480aa2b

See more details on using hashes here.

Supported by

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