requests.post 可以在慢速(但未死)连接上超时吗?

Can requests.post time out on a slow (but not dead) connection?

我正在使用 Raspberry Pi 3 和 Raspbian 8 (Jessie) 到 post 数据到使用单元格连接的服务器,这可能不可靠。我有一个棘手的错误,发送数据的线程挂起但没有超时,因此数据 posting 完全暂停很长一段时间,没有日志记录等。

我通过人为地显着降低 Pi 的连接速度(使用 tc/netem 来增加大约 20 秒的延迟)来复制这个。这样做时,请求将挂起比超时时间长得多的时间。我假设(但不确定)这是因为只有很少的数据包在慢速连接上通过。如果发生这种情况,有没有办法引发错误?还是我的假设有误?

请求代码:

result = requests.post(target_url, json=data_dict, timeout=5)

谢谢

根据 docs:

timeout is not a time limit on the entire response download; rather, an exception is raised if the server has not issued a response for timeout seconds (more precisely, if no bytes have been received on the underlying socket for timeout seconds).

换句话说,您看到的行为是预料之中的——只要服务器在超时到期之前开始发出响应,就不会触发超时。