When building a Flask application, what is the right place to instantiate spaCy, or, for that matter any memory-intensive object to be used in requests?
The problem with spaCy is that it takes roughly 15 seconds to load and occupies quite a lot of memory. So it makes no sense to instantiate it for every request. On the upside, spaCy is presumably thread safe, so it could be instantiated once per process and used in all its threads.
I'm using Flask 0.12. I'm mostly repeating the flaskr demo app, with the factory function create_app()
that collects all the blueprints and returns the application object. There's also the wsgi.py
that creates the variable app
using the factory function. I use that file as the entry point for uWSGI.
So what is the most appropriate place to instantiate spaCy, so that it is visible in every request?
Should I just instantiate it in create_app()
and attach it to app
?