Python3多进程scaper:BrokenPipeError(断管)

2024-05-14 21:47:02 发布

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

我有一个python3.7.4脚本,使用多处理和线程技术来获取API。它使用一个GET请求来获取一个hashtag的统计信息,然后在该页面上收集更多的hashtags以添加到要从API中获取的列表中。它工作几分钟,然后开始打印许多信息:

Traceback (most recent call last):
  File "/home/user/.pyenv/versions/3.7.4/lib/python3.7/multiprocessing/queues.py", line 242, in _feed
    send_bytes(obj)
  File "/home/user/.pyenv/versions/3.7.4/lib/python3.7/multiprocessing/connection.py", line 200, in send_bytes
    self._send_bytes(m[offset:offset + size])
  File "/home/user/.pyenv/versions/3.7.4/lib/python3.7/multiprocessing/connection.py", line 404, in _send_bytes
    self._send(header + buf)
  File "/home/user/.pyenv/versions/3.7.4/lib/python3.7/multiprocessing/connection.py", line 368, in _send
    n = write(self._handle, buf)
BrokenPipeError: [Errno 32] Broken pipe

代码在这里:

^{pr2}$

以下是API数据示例:

[{
    "query": "inspiration",
    "exists": true,
    "nodes": [{
            "id": "dedication", 
            "relevance": 0.5637108009450206, 
            "weight": 0.45454545454545453, 
            "x": 0.21844181188954195, 
            "y": 0.05872978594377127
        }, 
        {
            "id": "ifbb", 
            "relevance": 0.41447057552244726, 
            "weight": 0.18181818181818182, 
            "x": 0.04110103005950671, 
            "y": 0.031126457980437047
    }],
    "edges": [{
            "a": "dedication", 
            "b": "ifbb", 
            "id": "dedication#ifbb", 
            "weight": 0.13807764918076493
        }, 
        {
            "a": "dedication", 
            "b": "cleaneating", 
            "id": "dedication#cleaneating", 
            "weight": 0.05179527283184793
    }]
}]

如何修复此错误?在

编辑:我也尝试过(但没有成功):

  • 在请求后添加延迟/超时。在
  • 使用更多try/except块。在
  • 将工作线程减少到100。在
  • 将请求超时设置为10。在

Tags: inpysendidpyenvhomebyteslib
1条回答
网友
1楼 · 发布于 2024-05-14 21:47:02

您可能会受到速率限制,因为您发送的请求如此之快。尝试在请求之间添加延迟。参见: https://stackoverflow.com/a/28040423/9872244-如果您的请求被阻止或时间过长,并且请求端超时后,它将关闭连接,然后,当响应端(服务器)尝试写入套接字时,它将抛出管道断开错误。在

相关问题 更多 >

    热门问题