避免长时间运行的POST请求中的服务器超时

2024-10-01 17:31:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用pythonrequests库发送POST请求并从迭代器读取数据。在

如果迭代器很快生成数据,那么所有的工作都非常完美。在

问题是我的迭代器有时会“中断”,服务器由于不活动而断开连接,我的客户机在套接字级别引发Broken pipe异常。我不能避免源迭代器上的暂停,因为它们依赖于计算时间。在

服务器是SOLR,但我认为这应该是一个更一般的问题。在

更准确地说,情况是这样的(我之所以简化,是因为原始代码相当复杂):

import requests
def myObjectsIter():
    #this generator yields custom objects and sometimes pauses

def myObjectsSerializerIter():
    yield "<add>"
    for o in myObjectsIter():
        #o.serialize returns an XML string for SOLR update (<doc>...</doc>)
        yield o.serialize()
    yield "</add>"

requests.post("http://mysolr/mycore/update", data=myObjectsSerializerIter())

#When myObjectsIter pauses for more than (i think) 60 seconds the last call raises [Errno 32] Broken pipe

有人知道避免这种情况的好办法吗?在


Tags: 服务器addfordef情况updaterequestsserialize

热门问题