Flask-Threads is a helper library designed to simplify working with threads within Flask applications. It addresses the common challenge of maintaining the Flask application context (e.g., `flask.g`, `request`) when executing code in background threads or using concurrent futures, which are typically thread-local. The library ensures that thread-local proxies remain accessible, preventing `RuntimeError` exceptions that occur when trying to access context outside the main request thread. The current version is 0.2.0, released on May 20, 2025, with an infrequent release cadence, primarily focusing on Flask compatibility.
pip install Flask-ThreadsVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to use `AppContextThread` and `ThreadPoolWithAppContextExecutor` to run background tasks while retaining access to Flask's application context, specifically `flask.g`. The `user-id` is set in `flask.g` within the main request thread and then accessed correctly by the function running in a separate thread.
Upgrade to Flask-Threads version 0.2.0 or newer: `pip install --upgrade Flask-Threads`. Ensure your Flask version is compatible.
To prevent this in development, run your Flask application with `app.run(debug=True, use_reloader=False)`. For production, use a WSGI server like Gunicorn or uWSGI, which manage processes differently and typically don't have this issue.
Only access context-local objects in background threads for read-only purposes or when `flask-threads` explicitly manages the context copy. For complex, long-running background jobs, consider external task queues like Celery or RQ, and pass only plain, serializable data (like IDs or file paths) rather than Flask context objects.
For CPU-bound tasks, consider using multi-processing (e.g., Python's `multiprocessing` module or `concurrent.futures.ProcessPoolExecutor`), or dedicated worker queues, which can leverage multiple CPU cores.
Wrap the code that accesses application context in the background thread with `app.app_context()` or use `flask-threads`'s `AppContextThread` or `ThreadPoolWithAppContextExecutor` to automatically manage the context. For example: `from flaskthreads import AppContextThread; t = AppContextThread(target=my_function, args=(app,)).start()`
Use `flask-threads`'s `AppContextThread` or `ThreadPoolWithAppContextExecutor`, which ensure that the request context from the original thread is properly propagated to the background thread. Alternatively, pass necessary data extracted from `request` or `g` as arguments to your background function, avoiding direct context access in the thread.
Ensure the library is installed in your active Python environment using `pip install Flask-Threads`. Double-check your import statement for typos, e.g., `from flaskthreads import AppContextThread`.
When you need the actual application object from within a context, use `current_app._get_current_object()`. When passing the application to a background thread, if not using `flask-threads`, ensure you pass the actual application instance (`app`) or handle the context explicitly within the thread using `app.app_context()`.