微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无限期结束代码的 Python 中的 MemoryError

如何解决无限期结束代码的 Python 中的 MemoryError

我正在尝试编写一个后台无限期运行的 Python 程序(直到用户停止它)并检测对网页的任何更改。所以我散列页面内容并与之前的散列进行比较,如果值发生变化,那么我知道页面已经更改。这是我的代码

import requests
import threading
import hashlib
from urllib.request import urlopen


URL = 'https://www.bbc.co.uk/sport/live/football/55864099'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/83.0.4103.61 Safari/537.36'}
current_hash = 'Initial'

def monitor():
    threading.Timer(5,monitor).start() #The number in the Timer() bracket represents the scheduling interval in seconds
    response = urlopen(URL).read()
    new_hash = hashlib.sha224(response).hexdigest()
    global current_hash
    if(current_hash == new_hash):
        print('same')
    else:
        print('different')
        current_hash = new_hash

monitor()

但是,运行几分钟后,我在控制台中收到此错误

MemoryError

但有时程序会在短暂延迟后继续。有没有办法在我继续的过程中清除内存以防止发生此错误

解决方法

从 python 文档中,如果有什么东西关闭了,你应该调用 close 或者只是遭受 MemoryErrors,或者任何需要清理的资源。只有在阅读了 close() 方法背后的源代码并确定它不执行任何操作后,您才能真正跳过调用 close。

with urllib.request.urlopen('http://www.python.org/') as f:
...     print(f.read(300))

您还应该遵循指数退避,固定间隔轮询只是让您的 CPU 保持温暖并缩短其使用寿命,一个库可以做到这一点:https://pypi.org/project/backoff/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。