[Flask] Parallel execution of a single request

Miguel Lacerda mlacerda007 at gmail.com
Thu May 24 09:00:48 EDT 2018


Hi everyone

Is it possible to run the computations of a single request to a flask web
app across multiple CPUs? There is plenty of information on running
multiple requests in parallel, but nothing that I could find on
parallelizing the computation in a single request. Here is a toy example to
illustrate what I am trying to do:

from flask import Flaskfrom joblib import Parallel, delayedfrom time
import sleep
def myfunc(x):
    sleep(5)
    return x

application = Flask(__name__)
@application.route('/', methods = ['GET'])def getresult():
    out = Parallel(n_jobs=-1, verbose=10)(delayed(myfunc)(i) for i in range(5))
    return str(sum(out))
if __name__ == "__main__":
    application.debug = True
    application.run()


This produces the following warning:

UserWarning: Multiprocessing-backed parallel loops cannot be nested
below threads, setting n_jobs=1

and consequently does not run in parallel.

To give you some context, the version of myfunc that I am actually using
makes millions of predictions based on a machine learning algorithm (each
of which can be produced independently) that are then ranked and returned
by the application. My intention is to deploy the app on an Amazon EC2
instance with many CPUs so that the predictions can be performed quickly in
parallel.

Any assistance would be greatly appreciated!

Miguel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20180524/8b01a085/attachment.html>


More information about the Flask mailing list