我试着做一个程序,每小时做一些事情,然后自己运行,然后自杀。在
我遇到的问题是程序并没有完全自杀。当我使用系统监视器时,我看到进程没有消失。在
随着时间的推移,越来越多的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
据我所知,将启动一个子流程,并在完成后返回。这是一个阻塞操作,因为python将等待您启动的子进程完成,然后再执行任何其他代码。在操作系统()将显示程序从未到达
sys.exit(1)
我在尝试制作一个更好的crontab版本时也遇到了同样的问题。我修改了我的代码,这样你就能理解这个方法了。使用此方法,您不会遇到任何max递归问题。
如果您想让它24/7执行,只需将它放入
while True
循环。如果要限制速度,请导入时间模块。相关问题 更多 >
编程相关推荐