gpt4 book ai didi

python - 从 Flask 请求获取数据很慢

转载 作者:行者123 更新时间:2023-12-01 09:02:53 26 4
gpt4 key购买 nike

我有一个设备向我的服务器发送 POST。在 python 中,我使用以下任一方法获取发布的数据:

request.data
request.get_data
request.get_json

但是我获取这些简单数据的时间各不相同。我用这样的东西进行测试:

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

对于相同数量的数据,从某些终端节点,我看到时间低于 1 毫秒,而在某些终端节点,我看到时间超过 100 毫秒。由于请求对象已经创建,并且我假设已经接收到数据,那么是什么导致了这种速度差异?

最佳答案

and I assume the data is already received

不要假设:)

Flask 使用来自 Werkzeug which calls get_data 的请求类。 get_data 要么已经缓存了数据,要么 reads the stream .

文档字符串中甚至有一个警告:

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.

如果你想避免阻塞服务器缓慢的请求,你应该在它前面放置一些反向代理(nginx?),它可以更有效地处理输入,并在准备好时将完整的请求移交给服务器.

关于python - 从 Flask 请求获取数据很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52322923/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com