我想从url获取和使用数据,然后重复一个循环。当我运行代码时,我的操作内存使用量(MacOS)随着每个循环的增加而增加。我怎样才能防止这种情况呢?
提前谢谢你!
import urllib.request, json
import time
import resource
import gc
import contextlib
def bb():
try:
with contextlib.closing(urllib.request.urlopen("https://bitbay.net/API/Public/ETHPLN/ticker.json")) as url:
data = json.loads(url.read().decode())
## do something with data
del data
gc.collect()
except urllib.error.HTTPError as err:
pass
def resident_memory() -> int:
return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
x = 0
while x == 0:
bb()
print('Mem usage:', resident_memory())
time.sleep(1)发布于 2019-08-18 02:42:48
我在Python 3.7.4中遇到了同样的问题。我最终改用urllib3 (https://urllib3.readthedocs.io/en/latest/),这个问题很快就消失了。另外,我将我的HTTP PoolManager放在了循环的范围之外。
https://stackoverflow.com/questions/46962144
复制相似问题