简体   繁体   English

如何防止永远循环使用我所有 ram 的 python 脚本?

[英]How would I prevent a python script that loops forever from using all my ram?

I want to automate the process of clicking a few buttons in a game, so I wrote a script that looks for the first button and clicks it, then clicks the other two.我想自动化在游戏中单击几个按钮的过程,所以我编写了一个脚本来查找第一个按钮并单击它,然后单击另外两个。

import pyautogui as pag
import time

while True:
    if Click("Replay") == True:
        time.sleep(1)
        Click("Continue")
        time.sleep(1)
        Click("Repeat")
        time.sleep(1)
    else:
        time.sleep(5)

The Click() function basically takes a string input and turns it into input.png, matches it to a png i have of the buttons, and clicks it. Click() function 基本上接受一个字符串输入并将其转换为 input.png,将其与我拥有的按钮的 png 匹配,然后单击它。 If it doesn't find it, it just says it couldnt find it.如果它没有找到它,它只是说它找不到它。

Heres that in case you need it:继承人,以防你需要它:

def Click(image):
    image = str(image)
    location = pag.locateOnScreen(image + ".png")
    if location != None:
        point = pag.center (location)
        coordsX, coordsY = point
        pag.click(coordsX, coordsY)
        print("Clicked %s" % (image))
        return True
    else:
        print("Did not find %s Button" % (image))

My end goal was to make it into an executable so I could send it to people and they could use it, but I don't want it to waste a lot of memory on the computers it runs on.我的最终目标是将它变成一个可执行文件,以便我可以将它发送给人们并且他们可以使用它,但我不希望它在运行它的计算机上浪费大量 memory。 I also don't want to increase the wait time on it, and I believe I saw somewhere it clears itself after 2GB or something, but I'm not that good at coding and I dont want it using that much ram in the first place.我也不想增加它的等待时间,而且我相信我在某处看到它在 2GB 或其他东西后自行清除,但我不擅长编码,我不希望它首先使用那么多内存. Is there anything I can do to either A. clear it at a lower threshold, or B. make it not use that much ram in the first place?我能做些什么来 A. 以较低的阈值清除它,或者 B. 首先让它不使用那么多内存?

I did something similar before, using pyautogui.我之前做过类似的事情,使用 pyautogui。 I didn't see any performance issue when running similar code even after a few weeks.即使在几周后运行类似的代码时,我也没有看到任何性能问题。

You might want to try that out first as I doubt it will use that many RAM.您可能想先尝试一下,因为我怀疑它会使用那么多 RAM。

A few optimizations/cleanups to your code:对您的代码进行一些优化/清理:

  1. You don't need the else for time.sleep(5) . time.sleep(5)不需要 else 。
  2. Consider adding a return False to your Click() function's else clause?考虑在Click()函数的 else 子句中添加return False吗?
  3. Pyautogui's website says that the locateOnScreen function will raise an error now, not return None , so consider using a try-except block. Pyautogui 的网站locateOnScreen function 现在会引发错误,而不是返回None ,因此请考虑使用 try-except 块。 (If it doesn't return an exception for you, refer to the accepted answer to this question .) (如果它没有为您返回异常,请参阅此问题的已接受答案。)
  4. Pyautogui allows clicking a Point object, so use this instead: Pyautogui 允许单击点 object,因此请改用:
def Click(image):
    image = str(image)
    try:
        pag.click(pag.locateCenterOnScreen(image + ".png"))
        print("Clicked %s" % (image))
        return True
    except pag.ImageNotFoundException:
        print("Did not find %s Button" % (image))
        return False

I've run this code,and it uses almost no RAM for me.我已经运行了这段代码,它对我来说几乎没有使用 RAM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM