Sharing global information across requests to flask endpoints on heroku
<p>I have a flask application with a single endpoint, like this (simplified below):</p>

<pre><code>@app.route('/classify', methods=['POST'])
def classify():
    p = g.model_loader.get_model()
    json = request.get_json()
    text = json['text']
    return p.classify(text)


def main():
    model_loader = ResourceLoader()
    with app.app_context():
        g.model_loader = model_loader
        app.run()


if __name__ == '__main__':
    main()
</code></pre>

<p>The application needs to load a machine learning model into memory once, <strong>in the <code>main</code> method</strong>, and then use that model to classify text that is being sent via POST to a flask endpoint. I've tried to do this using an application context, which works locally, but doesn't work on heroku. My understanding currently is that this is because the application context isn't shared across workers. How can I give the <code>classify</code> endpoint access to the model loader?</p>

<p>EDIT: I didn't word the initial question correctly. Each worker should run <code>main()</code> and should thus have access to <code>model_loader</code> in its own application context. However, when I run this on heroku, I get <code>AttributeError: '_AppCtxGlobals' object has no attribute 'model_loader'</code>. Does the application context differ on heroku?</p>

<p>You can't load it into memory once, because Heroku workers might be on completely different machines. You need to run this code in every worker, or store the data somewhere it can be read by any process.</p>

python heroku flask