Skip to main content

An easy to use Flask wrapper for concurrent.futures

Project description

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'

Jobs submitted to the executor are wrapped with the current app and executed inside the app context so that tasks requiring an app context can run outside your main thread or process without requiring you to modify the function to handle this yourself.

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'

ThreadPoolExecutor jobs are also wrapped with a copy of the current app object and executed inside its app context, so that tasks requiring an app context can run outside your main thread.

Note: decoration is not currently supported when using a ProcessPoolExecutor, due to the multiprocessing library's inability to work with decorated functions.

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.3.1.tar.gz (3.7 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.3.1-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: Flask-Executor-0.3.1.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for Flask-Executor-0.3.1.tar.gz
Algorithm Hash digest
SHA256 b2b30231cf30397e69bf7a321fec8c35fc042bacc5a8d68ee126119f8b3b3d1d
MD5 8aa989cccefe643b29e032040ecfb0dd
BLAKE2b-256 70e2f1aaf194fbf42b9cea2600f9bf39a8b9da45981a5132e35a9ec64acb1203

See more details on using hashes here.

File details

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

File metadata

  • Download URL: Flask_Executor-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for Flask_Executor-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c2adcec6df0f3cc1fba20b32d4010717fb69d8c66e7f0fa8a8f0f61dfe237b7c
MD5 58de81d95de7f467d913da0cd3955577
BLAKE2b-256 019dada6a160463bb72a80a4d25e9b91d6c19348871ae9636fec9f334ecc2cac

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