[英]driver.quit() after unhandled exception
I need some help with this issue that I'm having.我需要一些帮助来解决我遇到的这个问题。
I have a python script that I'm using to keep another python script running on a loop.我有一个 python 脚本,我用它来保持另一个 python 脚本在循环上运行。
I basically call it like this python loop.py script.py
我基本上这样称呼它
python loop.py script.py
The script.py
uses selenium and chromedriver to perform whatever it needs to do, I'm trying to keep this script running 24/7, so whenever it fails, the loop script just restarts it and opens another instance of the chromedriver, opening another chrome window. script.py
使用 selenium 和 chromedriver 来执行它需要做的任何事情,我试图让这个脚本 24/7 运行,所以每当它失败时,循环脚本就会重新启动它并打开 chromedriver 的另一个实例,打开另一个铬 window。
My problem is that whenever I keep this script running for several hours, there's a point where I have too many chrome windows opened, causing the computer to go crazy slow as you would expect.我的问题是,每当我让这个脚本运行几个小时时,都会有一个点,我打开了太多的 chrome windows,导致计算机像你期望的那样疯狂地慢到 go。
script.py
is divided into multiple functions, with one function calling some of the other functions like: script.py
分为多个函数,其中一个 function 调用其他一些函数,例如:
def caller_function():
function1()
function2()
function3()
function4()
I already have some of the exceptions handled so basically the scripts only stops whenever an unhandled exception occurs.我已经处理了一些异常,所以基本上脚本只会在发生未处理的异常时停止。
This unhandled exception can happen in any of the functions so I can't seem to find a way around handling(?) the unhandled exception to make it do a driver.quit()
so it closes the instance that failed before the loop.py
calls script.py
again and opens another chrome window.这个未处理的异常可能发生在任何函数中,所以我似乎无法找到一种方法来处理(?)未处理的异常以使其执行
driver.quit()
所以它关闭在loop.py
之前失败的实例再次调用script.py
并打开另一个 chrome window。
I tried doing:我试着做:
def caller_function():
try:
function1()
function2()
function3()
function4()
except:
driver.quit()
But it didn't work, as for some reason the driver.quit() fell into a loop and stopped loop.py
from restarting script.py
但它不起作用,因为某种原因 driver.quit() 陷入循环并停止
loop.py
重新启动script.py
Basically, I'm trying not to have 20+ chrome windows open whenever I wake up in the morning to check the progress of the script.基本上,每当我早上醒来检查脚本的进度时,我都尽量不打开 20+ chrome windows。 Does anyone know a way around this?
有谁知道解决这个问题的方法?
use finally for that purpose: finally 用于此目的:
try:
# your code
finally:
driver.quit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.