How to use threads within a Flask route How to use threads within a Flask route flask flask

How to use threads within a Flask route


I think you will get close to your implementation using a multiprocessing.Pool.

You can set up a pool of workers as follows:

from multiprocessing import Poolpool = Pool(processes=31)

Then all your route needs to do is submit the jobs to the pool and wait for all of them to be done. I can't test this because you haven't provided enough code, but it may look more or less like this:

def sentence_numfound(path):    return jsonify(pool.map(foo, get_queries(path)))

This basically calls foo(query) for each query in the processes owned by the pool, all in parallel. The map() call will return when all the jobs are done. The return value is an array with the results, in the same order as the input array.

I hope this helps!