Skip to main content

Flask-Executor

Build 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.

Setup

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

pip install flask-executor

The Executor extension can either be initialized directly:

from flask import Flask
from flask_executor import Executor

app = Flask(__name__)

executor = Executor(app)

Or through the factory method:

executor = Executor()

executor.init_app(app)

Configuration

app.config['EXECUTOR_TYPE']

Specify which kind of executor to initialise. Valid values are 'thread' (default) to initialise a concurrent.futures.ThreadPoolExecutor, or 'process' to initialise a concurrent.futures.ProcessPoolExecutor.

app.config['EXECUTOR_MAX_WORKERS']

Define the number of worker threads for a ThreadPoolExecutor or the number of worker processes for a ProcessPoolExecutor. Valid values are any integer or None (default) to let the concurrent.futures module pick defaults for you.

Usage

You can submit examples to the executor just as you would expect:

from flask import Flask
from flask_executor import Executor

app = Flask(__name__)
executor = Executor(app)

def fib(n):
    if n <= 2:
        return 1
    else:
        return fib(n-1) + fib(n-2)

@app.route('/example1')
def example1():
    executor.submit(fib, 5)
    return 'OK'

Submitting examples to the executor returns standard concurrent.futures.Future objects that you can work with:

import concurrent.futures
from flask import Response

@app.route('/example2')
def example2():
    future = executor.submit(fib, 5)
    return str(future.result())

@app.route('/example3')
def example3():
    futures = [executor.submit(fib, i) for i in range(1, 40)]
    def generate():
        for future in concurrent.futures.as_completed(futures):
            yield str(future.result()) + '\n'
    return Response(generate(), mimetype='text/text')

If you're using a ThreadPoolExecutor, you can use a decorator pattern in the style of Celery or Dramatiq to decorate functions which can then be submitted to the executor directly:

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

@app.route('/example4')
def example4():
    fib.submit(5)
    return 'OK'

Metadata

Release files for Flask-Executor 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Flask-Executor 0.2.0
File Size Uploaded Provenance Actions
Flask-Executor-0.2.0.tar.gz 3.2 kB Without provenance Details for Flask-Executor-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for Flask-Executor 0.2.0
File Interpreter ABI Platform Actions
Flask_Executor-0.2.0-py3-none-any.whl Python 3 none any Details for Flask_Executor-0.2.0-py3-none-any.whl

Total release size: 6.3 kB

Release files / Flask-Executor-0.2.0.tar.gz

Download URL Flask-Executor-0.2.0.tar.gz
Size 3.2 kB
Tags Source
SHA-256 checksum
How to use checksums
32d02db1e4917e3949378711e5644a4d879128fe1a9e21b65c5d6a6f2ffdc091
BLAKE2b-256 checksum
How to use checksums
f03ba83cb66c659ff4abab068991ab00a4057a65c20e4f6011b708c48fdedf6f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.0

Release files / Flask_Executor-0.2.0-py3-none-any.whl

Download URL Flask_Executor-0.2.0-py3-none-any.whl
Size 3.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4700199db97ee4e3227eacc173bbb93c7c332b2dc7e127979a377f8441f5ba52
BLAKE2b-256 checksum
How to use checksums
fd0ab6644a5d7046d9409093c1e91cebeb82b734fe754afc781010b237bc5304
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.0

Security for Flask-Executor 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page