每當我運行以下檔案(并且 main 遇到 WebDriverException 例外)時,我的程式都會結束而不是重新啟動。有人知道為什么會這樣嗎?任何幫助將不勝感激——謝謝。
from uploadToBeatstars import main
from selenium.common.exceptions import WebDriverException
try:
main()
except WebDriverException:
print("Dumb target error happened. Restarting program.")
from uploadToBeatstars import driver
driver.close()
import sys
import os
os.execv(sys.executable, ['python'] sys.argv)
uj5u.com熱心網友回復:
通常,您不需要在失敗后重新生成解釋器,只需在回圈中重試即可:
from uploadToBeatstars import main, driver
from selenium.common.exceptions import WebDriverException
while True:
try:
main()
except WebDriverException:
print("Dumb target error happened. Restarting program.")
driver.close() # Not sure if it is needed, can driver be alive after an exception?
# Try again
else:
break # Stop if no exception occurred.
uj5u.com熱心網友回復:
在 Windows 上,os.exec*函式族不像在 posixlikes 上那樣運行——它們不是替換當前行程,而是在后臺生成一個新行程,并且os._exit(1)
更多資訊在這里:https ://github.com/python/cpython/issues/53394
在 Windows 上, exec() 并沒有真正替換當前行程。它創建一個新行程(使用新的 pid),并退出當前行程。
您可能最好撰寫一個回圈或使用某種流程管理器
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/479808.html
