2016-10-24 11 views
0

私は毎時間何かをしてから、それ自身を実行してから、自分自身を殺すプログラムを作ろうとしています。sys.exit()殺していないプロセス

私が抱えている問題は、プログラムが完全に自分自身を殺さないということです。システムモニタを使用すると、プロセスが終了しないことがわかります。

Overtime私は、ramを使うpython2プロセスがますます増えています。

私はアーチ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 
+0

読んでください、http://stackoverflow.com/questions/19747371/python-exit-commands-why-so-many-and-when-should-each-be-used –

+0

私はこれに驚いています '尋ねられましたが、なぜですか?あなたはあなたのプログラムを殺し/再建することからどんな利益を得ますか?デーモンを作るだけで何ができないのですか?あなたはプロセスを再構築することで何を達成しようとしていますか? – TemporalWolf

答えて

0

を実行しているコードです一度それが帰りを完了した。これは、Pythonが起動したサブプロセスが他のコードを実行する前に完了するのを待つため、ブロックアクションです。 os.system()の後にprintステートメントを追加すると、プログラムがsys.exit(1)に届かないことがわかります

0

より良いバージョンのcrontabを作成しようとしたときと全く同じ問題がありました。私はあなたのアプローチを理解するように私のコードを変更しました。この方法では、最大再帰問題に遭遇することはありません。

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時間365日実行する場合は、ループをwhile Trueループに入れるだけです。あなたは速度を制限したい場合は、インポート時間モジュール。

関連する問題