You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python通过ping检测控制while循环内函数:通则运行、断则停止

提问内容

我想要实现ping检测功能:如果ping成功,就调用一个函数并持续运行该函数,直到ping失败为止。如果调用函数后ping变为不成功,则停止函数运行,直到ping恢复成功。

流程示意图

到目前为止,这是我尝试的代码。ping测试运行良好,但是while循环会在运行完函数内所有代码后退出自动化函数(关闭浏览器)。我希望能持续运行该函数,仅在ping失败时才停止。

import urllib
from selenium import webdriver


def ping(host="8.8.8.8", port=53, timeout=3):
    from urllib import request
    try:
        urllib.request.urlopen('http://google.com', timeout=5)
        return True
    except:
        return False

def automation():
     url1 = "https://google.com"
     driver=webdriver.Chrome()
     driver.get(url1)



run_once = 0
while True:
        if ping():
                if run_once == 0:
                        automation()
                        run_once = 1
        else: 
                print("ping unsuccessful")
                run_once = 0

更新

感谢用户@questioning的回答,以下是对我有效的解决方案。我还使用了drive.refersh来节省电量,因为我的使用场景涉及摄像头,在while循环前调用driver可以让浏览器和摄像头即使在连接断开后仍保持运行。

import urllib
from selenium import webdriver

Global driver
driver=webdriver.Chrome()

def ping(host="8.8.8.8", port=53, timeout=3):
    from urllib import request
    try:
        urllib.request.urlopen('http://google.com', timeout=5)
        return True
    except:
        return False

def automation():
     url1 = "https://google.com"
     driver=webdriver.Chrome()
     driver.get(url1)



run_once = 0
reload_once = 0
while True:
        if ping():
                if run_once == 0:
                        automation()
                        run_once = 1
        else: 
                print("ping unsuccessful")
                run_once = 0
                if reload_once == 0:
                        drive.refersh()
                        reload_once == 1

解答

问题原因

你的代码核心问题有两处:

  1. 初始版本中automation函数内的driver是局部变量,函数执行完成后变量被回收,对应浏览器进程自动关闭。
  2. 你更新的版本存在几处笔误:Global关键字需要小写为global、drive.refersh应为driver.refresh()、reload_once == 1是比较操作,应该改为赋值操作reload_once = 1,同时你在automation函数里重复初始化了driver,会导致多开浏览器窗口。

修正后代码

import urllib
from selenium import webdriver
import time

# 全局管理driver实例,避免重复初始化
driver = webdriver.Chrome()

def ping(host="8.8.8.8", port=53, timeout=3):
    try:
        urllib.request.urlopen('http://google.com', timeout=5)
        return True
    except:
        return False

def automation():
    url1 = "https://google.com"
    driver.get(url1)
    # 你可以在这里添加需要持续执行的自动化操作逻辑

run_once = 0
while True:
    # 增加1秒延迟,避免CPU占用过高
    time.sleep(1)
    if ping():
        if run_once == 0:
            automation()
            run_once = 1
        # 后续需要持续运行的逻辑写在这里即可
        # 示例:定时刷新页面节省电量
        # if int(time.time()) % 60 == 0:
        #     driver.refresh()
    else:
        print("ping unsuccessful")
        run_once = 0

说明

  • 全局初始化driver的方式符合你需要保持摄像头运行的需求,ping失败时不会销毁浏览器实例,网络恢复后可以直接复用。
  • 所有需要持续执行的自动化逻辑都放在ping成功的分支下,只有ping失败时才会停止运行。

内容的提问来源于stack exchange,提问作者Ahmed Osman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 02:36:01