Skip to content

Instantly share code, notes, and snippets.

@gdamjan
Last active January 11, 2021 06:52
Show Gist options
  • Save gdamjan/9936243 to your computer and use it in GitHub Desktop.
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
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
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
[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
@gdamjan
Copy link
Author

gdamjan commented Apr 2, 2014

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:

@app.before_first_request
def initialize():
    app.pool = ThreadPool(10)

and then use app.pool instead of the global pool

@gdamjan
Copy link
Author

gdamjan commented Apr 2, 2014

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