Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python sys.exit()未终止进程_Python_Python 2.7_Selenium - Fatal编程技术网

Python sys.exit()未终止进程

Python sys.exit()未终止进程,python,python-2.7,selenium,Python,Python 2.7,Selenium,我试图制作一个每小时做一件事的程序,然后自动运行,然后自杀 我遇到的问题是,这个程序并没有完全自杀。当我使用系统监视器时,我发现这个过程并没有消失 随着时间的推移,我得到了越来越多占用ram的python2进程 我正在运行ArchLinux的64位机器上使用Python 2.7.12 这是我正在运行的代码 def GoToWebsite(username, password): chrome_options = webdriver.ChromeOptions() prefs =

我试图制作一个每小时做一件事的程序,然后自动运行,然后自杀

我遇到的问题是,这个程序并没有完全自杀。当我使用系统监视器时,我发现这个过程并没有消失

随着时间的推移,我得到了越来越多占用ram的python2进程

我正在运行ArchLinux的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

据我所知,子流程将启动,一旦完成返回。这是一个阻塞操作,因为python将在执行任何其他代码之前等待您启动的子进程完成。在os.system()之后添加一个print语句将显示程序从未到达
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

如果希望它全天候执行,只需将其放入
while True
循环中即可。如果要限制速度,请导入时间模块。

请阅读,我很惊讶没有人问这个问题,但为什么?您从终止/恢复您的计划中获得了什么好处?是什么阻止你仅仅创建一个守护进程?通过重新启动流程,您试图实现什么?