Is there a way (in uWSGI or Flask) to register a function to be run in each worker after spawning but as soon as possible?
We have a flask app that benefits from preloading a bunch of stuff. The following only calls preload
once for the 8 worker processes. The first request is fast, but the preloaded objects are shared in some fashion that causes errors.
app = Flask(__name__)
preload()
If I use before_first_request, then the objects are loaded in each worker process and there are no errors, but the first request is very slow.
app = Flask(__name__)
@app.before_first_request
def bfr():
preload()
I also tried setting up a flask script. Running the command works, but obviously the objects are loaded in the command's process rather than the uwsgi workers.
app = Flask(__name__)
manager = Manager(app)
@manager.command
def preload():
...
I guess if we use before_first_request
, we could manually trigger a request after restarting uwsgi. Is that the only solution here?
edit: Just found the uswgi hook-post-fork option (and other hook options). I'm going to try that tomorrow. Maybe one of those is what I need.