python - 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?
解决方案
我假设数据已经收到
不要假设:)
Flask 使用来自 Werkzeug 的请求类,它调用get_data
. get_data
要么已经缓存了数据,要么读取了流。
文档字符串中甚至还有一个警告:
通常不先检查内容长度就调用此方法是个坏主意,因为客户端可能会发送数十兆字节或更多字节,从而导致服务器出现内存问题。
如果您想安全地避免在缓慢的请求上阻塞您的服务器,您应该在它前面放置一些反向代理(nginx?),它可以更有效地处理输入,并在准备好时将完整的请求交给服务器。
推荐阅读
- android - Ionic - 无法通过注册表获取插件
- c# - 如何在c#中通过正则表达式模式将纯文本(带下划线)转换为超链接
- autodesk-forge - 如何使用 Autodesk A360 API 获取文档路径?
- firebase - QuerySnapshot 中的 fireStore 问题始终为空
- spring-data-jpa - 如何在 spring queryDSL 中为 List 构建谓词?
- c++ - 如何在等价列表中找到最小的连接标签
- for-loop - Oracle PL/SQL for 循环:根据条件选择查询
- ocaml - OCaml 中有没有办法在记录中声明一个整数,它是一些整数的间隔?
- laravel-7 - 我无法在 laravel 7 中重置密码
- pyspark - pyspark中for循环内的过滤器真的很慢