Skip to main content

A helper library to work with threads within Flask applications.

Project description

Flask-Threads

Actions Status

A helper library to work with threads within Flask applications.

The main problem that you face trying to spin a background thread or running a future in Flask app - is loosing the application context. The most common scenario is to try to access flask.g object. Application context is a thread local so you can not access it from another thread and Flask will raise an exception if you would try to.

This library provides helper classes that allows you accessing the current application context from another thread.

Warning! Alpha-version, use at your own risk.

Installation

$ pip install Flask-Threads

Examples

Threads

from flask import request
from flask import Flask
from flaskthreads import AppContextThread

app = Flask('my_app')


@app.route('/user')
def get_user():
    g.user_id = request.headers.get('user-id')
    t = AppContextThread(target=do_some_user_work_in_another_thread)
    t.start()
    t.join()
    return 'ok'


def do_some_user_work_in_another_thread():
    id = g.user_id
    print(id)

Concurrent futures

from flask import request
from flask import Flask
from flaskthreads import ThreadPoolWithAppContextExecutor

app = Flask('my_app')


@app.route('/user')
def get_user():
    g.user_id = request.headers.get('user-id')
    with ThreadPoolWithAppContextExecutor(max_workers=2) as pool:
        future = pool.submit(do_some_user_work_in_another_thread)
        future.result()
    return 'ok'


def do_some_user_work_in_another_thread():
    id = g.user_id
    print(id)

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-Threads-0.0.1a0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

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

Flask_Threads-0.0.1a0-py3.6.egg (7.5 kB view details)

Uploaded Egg

File details

Details for the file Flask-Threads-0.0.1a0.tar.gz.

File metadata

  • Download URL: Flask-Threads-0.0.1a0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8

File hashes

Hashes for Flask-Threads-0.0.1a0.tar.gz
Algorithm Hash digest
SHA256 388e1146be5ad6b051e13c282d34cac98a2db84ca16120d21759c7f395d67f3a
MD5 6dfaaab1b6de406e038e7b1159f9554c
BLAKE2b-256 729730531b963968308f7b119588a2cefa0cf33d90c41850dfbccb1f791a7090

See more details on using hashes here.

File details

Details for the file Flask_Threads-0.0.1a0-py3.6.egg.

File metadata

  • Download URL: Flask_Threads-0.0.1a0-py3.6.egg
  • Upload date:
  • Size: 7.5 kB
  • Tags: Egg
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.8

File hashes

Hashes for Flask_Threads-0.0.1a0-py3.6.egg
Algorithm Hash digest
SHA256 0409180fcde8dceab28d8dbfd4564c03359090dbb64d945b6b269673e55d80a8
MD5 2aaf3f1d0cc7cacdb1c52f23029d55d8
BLAKE2b-256 0293808402f79bca0abbfc8586e32954326a0249a58ebfb8e6bcb37f30675d36

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