gpt4 book ai didi

python - sys.exit() 不是终止进程

转载 作者:太空宇宙 更新时间:2023-11-03 16:02:27 27 4
gpt4 key购买 nike

我正在尝试制作一个程序,每小时执行一些操作,然后自行运行,然后自行终止。

我遇到的问题是该程序没有完全杀死自己。当我使用系统监视器时,我发现该进程没有消失。

随着时间的推移,我得到越来越多占用内存的 python2 进程。

我在运行 Arch Linux 的 64 位机器上使用 Python 2.7.12

这是我正在运行的代码

def GoToWebsite(username, password):
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
chrome_options.add_experimental_option("prefs", prefs)
chromeBrowser = webdriver.Chrome('/home/daniel/Dropbox/Code/BrowserDrivers/chromedriver',
chrome_options=chrome_options)
chromeBrowser.get('http://www.website.com')
while True:
try:
picX, picY = pyautogui.locateCenterOnScreen(currentPythonDirectory + '/picture.png')
break
except:
pass
pyautogui.click(picX, picY)
time.sleep(3)
url = chromeBrowser.command_executor._url
session_id = chromeBrowser.session_id
return url, session_id

websiteUrl, websiteSessionId = GoToWebsite("username", "password")
#Do Stuff
originalStartTime = time.time()
currentPythonDirectory = os.path.dirname(os.path.realpath(__file__))

while True:
if (time.time() - originalStartTime) >= 3: # 3600:
chromeDriver = webdriver.Remote(command_executor=websiteUrl, desired_capabilities={})
chromeDriver.session_id = websiteSessionId
chromeDriver.quit()
try:
chromeDriver.close()
except:
pass
os.system("python2 " + currentPythonDirectory + "/PythonScript.py")
time.sleep(1)
sys.exit(1)
break
#Other Stuff

最佳答案

在尝试制作更好版本的 crontabs 时,我遇到了完全相同的问题。我修改了我的代码,以便您了解该方法。使用这种方法,您将不会遇到任何最大递归问题。

import os, commands, regex, subprocess
from subprocess import call

allActivePythonProcesses = os.popen('pgrep -lf python').read()
thisIsYourPythonFileProcess = find('\d{7} python myRepeatingFile.py', allActivePythonProcesses )
if thisIsYourPythonFileProcess:
# Store the Process ID
convPID = find('\d{7}', thisIsYourPythonFileProcess)
print "Your Python File is running at PID: " + convPID
else:
print "Process Controller: Your Python file is not running"
try:
print "...Calling your Python file"
subprocess.check_call('python myRepeatingFile.py', shell=True)

except subprocess.CalledProcessError as e:
print "Process Call Error :" + e

如果您希望它 24/7 执行,只需将其放入 while True 循环中即可。如果要限速,请导入时间模块。

关于python - sys.exit() 不是终止进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40214374/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com