简体   繁体   中英

Getting data from flask request is slow

I have a device sending POSTs to my server. In python, I grab the posted data using either:

request.data
request.get_data
request.get_json

But the times I can getting for these simple data gets varies. I test with something like this:

start = time.time()
resp = request.data
return str(time.time() - start)

From some end nodes, I see times of sub 1ms, and some, I see over 100ms, for the same amount of data. Since the request object is already created, and I assume the data is already received, what contributes to this variance in speed?

and I assume the data is already received

Don't assume :)

Flask uses the request class from Werkzeug which calls get_data . get_data either already has the data cached, or reads the stream .

There's even a warning in the docstring:

Usually it's a bad idea to call this method without checking the content length first as a client could send dozens of megabytes or more to cause memory problems on the server.

If you want to be safe from blocking your server on a slow request, you should put some reverse proxy in front of it (nginx?) which can handle the input more efficiently and hand over a full request to the server when ready.

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