Python - aiohttp 请求连续超时

Python - aiohttp requests continuously time out

我有一个 Python 程序,它使用 aiohttp 和 ElementTree 从网站获取数据。下面的代码是 Raspberry Pi 上托管的 Discord 聊天机器人的一部分。大部分时间功能都很好,但是bot上线几天后,功能就开始卡顿,老是超时。重新启动程序并不能解决问题,只有重新启动 Pi 似乎可以暂时解决问题。我知道要继续进行的事情并不多,但是这段代码是否存在明显的问题可以导致这种情况,或者问题出在其他地方?

import lxml.etree as ET
import aiohttp, async_timeout
...

async with aiohttp.ClientSession() as session:
   try:
      with async_timeout.timeout(5):
         async with session.get('https://example.com', params=params, headers=headers) as resp:
            if resp.status == 200:
               root = ET.fromstring(await resp.text(), ET.HTMLParser())
               # Do stuff with root
            else:
               print("Error: {}".format(resp.response))
   except Exception as e:
      print("Timeout error {}".format(e))

也许某个地方的内存泄漏会慢慢耗尽系统内存,一旦满了,一切都会变得非常慢,因为交换用于内存分配并且会发生超时。

然而,正如安德鲁所说,这不是 python 脚本的问题,或者可以通过重新启动它来解决。

监控系统内存并从那里开始。