https://github.com/supercoderhawk/flask-threads
https://github.com/supercoderhawk/flask-threads
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/supercoderhawk/flask-threads
- Owner: supercoderhawk
- License: mit
- Created: 2025-03-04T09:04:22.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-03-04T09:07:31.000Z (about 1 year ago)
- Last Synced: 2025-03-04T10:23:12.440Z (about 1 year ago)
- Language: Python
- Size: 28.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
## Flask-Threads
[](https://github.com/sintezcs/flask-threads/actions)
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.**
**This is a extended version from https://github.com/sintezcs/flask-threads**
### Installation
```bash
$ pip install Flask-Threads-Ext
```
### Examples
#### Threads
```python
from flask import g
from flask import request
from flask import Flask
from flask_threads_ext 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
```python
from flask import g
from flask import request
from flask import Flask
from flask_threads_ext 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)
```