简体   繁体   中英

How can I speed up my Flask API that has a single slow method that gets parallel requests?

I have a single fuzzy match method that is taking around 5 seconds to complete. I have been asked to simulate many parallel requests (I am using jMeter for this). When I do this, the time it takes is linear to the amount of requests.

Is there a way for this method to run many times at once in order to speed up this API?

I looked into multi-processing and threading, but this seems to apply to running two methods concurrently. Not a single method concurrently over several API requests.

def fuzzy_match():
    #do 5-7 second calculation

Like I mentioned above, I would like an API that runs the parallel requests to this method faster. Right now, I am making 5 calls concurrently but will take 25 seconds to complete.

I should mention that I am running these on the development server. Would that be the issue? Any advice would be greatly appreciated.

Yes, you need to run the application with a proper wsgi server. As far as I remember the flask Dev server is not multithreaded. I would suggest installing gunicorn and running it with many worker threads which will allow for concurrent calls to your fuzzy match. It's also worth noting that long running tasks should sometimes be pushed off to asychronous services like celery to avoid overloading the web server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM