Python循环脚本占用的内存越来越大怎么解决

李英俊小朋友 2019-01-17 12:35:06
用python写了一个脚本。
用的是while True的循环。
基本内容是一直检测图像中某一个点的颜色,是否是某个颜色,如果是就进行操作。
运行的时候,打开任务管理器,可以看到Python.exe的内存越来越大。
过一段时间,脚本就不起作用了,卡住了。
用了多线程的方式,也没法解决。
请问有什么好的办法吗。
...全文
6725 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
pqwer99 2021-01-19
  • 打赏
  • 举报
回复 2
以前也遇到这样的问题,在函数shujucaiji里用while 1: 和 sleep,结果高CPU和高内存,后来把while和sleep去掉,用下面的方式就没问题了,可以参考一下。 # 设置定时器,每t秒采集一次 t = 15.0 cj = threading.Timer(t, shujucaiji) while 1: cj.run() cj.finished.clear()
geek_puddin 2021-01-19
  • 打赏
  • 举报
回复
试试用生成器呢
Daniel_wcy 2021-01-18
  • 打赏
  • 举报
回复
引用 5 楼 CalvinXu17 的回复:
import gc gc.collect()
没用
CalvinXu17 2019-12-20
  • 打赏
  • 举报
回复
import gc gc.collect()
linurm 2019-08-23
  • 打赏
  • 举报
回复 1
最快的解决办法就是让python运行一段时间,自动关掉,再重启,当然重启后可以接着你上次的运行
李英俊小朋友 2019-01-17
  • 打赏
  • 举报
回复
以下是代码: #!/usr/bin/env python # -*- coding: UTF-8 -*- # coding=utf-8 """ @author: Li Tian @contact: 694317828@qq.com @software: pycharm @file: hunshi_test3.py @time: 2019/1/12 20:20 @desc: 魂十测试3 """ import pyautogui as ptg import time import gc from threading import Thread import yinyangshi.parameters as par import yinyangshi.setting as sett from yinyangshi.baigui_test import click_motion, just_motion, click_small from yinyangshi.random_file import * import yinyangshi.baigui_test as ghost1 import yinyangshi.fight_for_boys as ffb def yaoqing1(): """接收邀请和自动邀请""" while sett.state: sett.yaoqing1 = ptg.pixelMatchesColor(par.hs_yaoqing_loc[0], par.hs_yaoqing_loc[1], par.hs_yaoqing_color) if sett.yaoqing1: click_small(par.hs_yaoqing_loc) time.sleep(pause_time() + 2) time.sleep(pause_time()) def yaoqing2(): """接收邀请和自动邀请""" while sett.state: sett.yaoqing2 = ptg.pixelMatchesColor(par.hunshi_yaoqing_loc[0], par.hunshi_yaoqing_loc[1], par.hunshi_yaoqing_color) if sett.yaoqing2: click_small(par.hunshi_yaoqing_loc) time.sleep(pause_time() + 2) time.sleep(pause_time()) def run1(): # 主循环 while sett.state: # 接受别人邀请的人,只需要点击结算界面 # 监测胜利还是失败 sett.hunshi_win = ptg.pixelMatchesColor(par.hunshi_loc[0], par.hunshi_loc[1], par.hunshi_color) if sett.hunshi_win and sett.state: # 战斗结束 # 点击领取奖励 click_motion(par.hunshi_loc) time.sleep(pause_time()) click_motion(par.hs_jiesuan_loc) time.sleep(pause_time()+2) time.sleep(pause_time()) def run2(): # 主循环 while sett.state: sett.hs_jiesuan = ptg.pixelMatchesColor(par.hs_jiesuan_loc[0], par.hs_jiesuan_loc[1], par.hs_jiesuan_color) if sett.hs_jiesuan and sett.state: # 领取奖励 # 点击返回 time.sleep(pause_time()+2) sett.number += 1 click_motion(par.hs_jiesuan_loc) time.sleep(pause_time()) click_motion(par.hs_jiesuan_loc) time.sleep(pause_time()) click_motion(par.hs_jiesuan_loc) time.sleep(pause_time()+3) # 魂十刷完了 print('完成第' + str(sett.number) + '次魂十。。。') time.sleep(pause_time()) def main(): gc.enable() threads_list = [] t1 = Thread(target=run1) threads_list.append(t1) t2 = Thread(target=run2) threads_list.append(t2) t3 = Thread(target=yaoqing2) threads_list.append(t3) t4 = Thread(target=yaoqing1) threads_list.append(t4) th = Thread(target=ghost1.hook) threads_list.append(th) t5 = Thread(target=ffb.run2) threads_list.append(t5) for i in threads_list: i.start() for j in threads_list: j.join() if __name__ == '__main__': main()

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧