Search code examples
pythonmultithreadingflaskserverwsgi

FLASK: Does the code outside the routers get executed every time a new request comes in?


The following is a basic flask app:

from flask import Flask, render_template, request
import pickle
import os

cur_dir = os.path.dirname(__file__)
clf = pickle.load(open(os.path.join(cur_dir, 'pkl_objects/classifier.pkl'), 'rb'))

@app.route('/')
def index():
    return "RESPONSE"

Now my question is whether the model is loaded every time a new request is made to this server or is it loaded just once and only the routes get executed for every incoming request? This is hard to figure out using a simple Flask development server since it contains only one thread. So if a deployment server spawns a thread for each request, will the loading of model happen each time?


Solution

  • Your code is a regular python code. What happens when you start the application is that your Python WSGI HTTP Server (there are plenty of WSGI servers like gunicorn) loads this script and verifies the minimal requirements for a WSGI Servers (which flask takes care of). See here for details. This server might start several instances of this script for performance purposes, then your model will be loaded several times.

    Then what happens when a request is made is that the server balances this request in one of the previously started process and access the flask object in it directly (so it doesn't reload the code). However, some servers might adapt the number of process depending on the number of request, and your model will be reloaded.