Last active
January 11, 2021 06:52
-
-
Save gdamjan/9936243 to your computer and use it in GitHub Desktop.
Creating a ThreadPool in parent process (like in the uwsgi prefork mode) fails later
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from multiprocessing.pool import ThreadPool | |
from time import sleep | |
app = Flask(__name__) | |
@app.before_first_request | |
def initialize(): | |
app.pool = ThreadPool(10) | |
@app.route("/") | |
def index(): | |
app.pool.apply_async(hello) | |
return 'ok' | |
def hello(): | |
for i in range(5): | |
print i | |
sleep(1) | |
app.debug = True | |
application = app |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from multiprocessing.pool import ThreadPool | |
from time import sleep | |
app = Flask(__name__) | |
pool = ThreadPool(10) | |
@app.route("/") | |
def index(): | |
pool.apply_async(hello) | |
return 'ok' | |
def hello(): | |
for i in range(5): | |
print i | |
sleep(1) | |
app.debug = True | |
if __name__ == "__main__": | |
app.run(host='0.0.0.0') | |
else: | |
application = app |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[uwsgi] | |
master = true | |
plugins = python | |
enable-threads = true | |
project_dir = %d | |
chdir = %(project_dir) | |
wsgi-file = %(project_dir)/App.wsgi | |
close-on-exec = true | |
http-socket = :5000 |
Also this will fail too:
pool = ThreadPool()
pid = os.fork()
if pid == 0: # child
pool.apply(...)
will not work. Some discussion here: http://bugs.python.org/issue6923
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem is that the pool is created on file import (in the master) and when the worker is forked, it doesn't inherit the threads.
the fix is to create the pool in the worker:
and then use app.pool instead of the global pool