系统出口()不是终止进程

2024-09-30 01:25:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着做一个程序,每小时做一些事情,然后自己运行,然后自杀。在

我遇到的问题是程序并没有完全自杀。当我使用系统监视器时,我看到进程没有消失。在

随着时间的推移,越来越多的python2进程占用了ram。在

我在运行archlinux的64位机器上使用python2.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

Tags: 程序idurltime进程ossessionchrome
2条回答

据我所知,将启动一个子流程,并在完成后返回。这是一个阻塞操作,因为python将等待您启动的子进程完成,然后再执行任何其他代码。在操作系统()将显示程序从未到达sys.exit(1)

我在尝试制作一个更好的crontab版本时也遇到了同样的问题。我修改了我的代码,这样你就能理解这个方法了。使用此方法,您不会遇到任何max递归问题。

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循环。如果要限制速度,请导入时间模块。

相关问题 更多 >

    热门问题