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.

Installation

$ pip install Flask-Threads

Examples

Threads

from flask import g
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 g
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.2.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

flask_threads-0.2.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file flask_threads-0.2.0.tar.gz.

File metadata

  • Download URL: flask_threads-0.2.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.6

File hashes

Hashes for flask_threads-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8dc6e8445cc8b97650f9c2b1a319b293a49e8f7e331b3d71dfe38a1f0b944fe5
MD5 e49d0a97c3e259b45c501ebc5fb25019
BLAKE2b-256 9639613f71271e9b69b277a8ebcc08fd5976e47ab4754ec15bc392b421f34e86

See more details on using hashes here.

File details

Details for the file flask_threads-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: flask_threads-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.6

File hashes

Hashes for flask_threads-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1778c7c474efec6e97a6673b61631864c4142ed6db9187d0f2affeaca11eea44
MD5 e71c44af3d367af614c67abd6c8b4c48
BLAKE2b-256 80c5be25f999a1dc4adde3228842114722efe9465c53e95be5c961f6b9e972d4

See more details on using hashes here.

Supported by

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