Using threads within a gunicorn flask worker that has `--threads=1`

Viewed 550

I have a flask app that has been deployed with a gunicorn server.

The exact command I use to start the gunicorn worker is gunicorn --workers 4 app:app. Therefore it is using the default sync workers and --threads=1.

Now in the flask app itself I have a particular route that needs to call a 3rd party API, while also doing some CPU work. Example:

@app.route("/")
def func():
    r = requests.get("www.google.com")
    x = 0
    for i in range(10):
        x += i
    print(r.content)
    return x

In theory, I thought it was obvious that I could have the get request as a task that runs in the background. I then do the CPU work and collect the result of the get request at the end. Something like this:

pool = concurrent.futures.ThreadPoolExecutor(max_workers=1)


@app.route("/")
def func():
    future = pool.submit(requests.get, "google.com")
    x = 0
    for i in range(10):
        x += i
    result = future.result()
    return x

This isn't giving me the speedup I was expecting.

Obviously the example I've given is a toy example. In reality, I call something else other than google. One thing I know is that the external request takes less time than the CPU intensive work going on here.

One hunch I have is that because gunicorn is set up with only 1 thread per worker, that I don't get to see any multithreaded benefit from this python code? Or is there something else that I'm missing?

0 Answers
Related