需要检查事件是否已经发生然后退出(无休止的)循环但程序必须 运行 连续
Need to check if an event is already happened then exit the (endless) loop but the program must run continously
我正在尝试在 Python 中编写一个小程序来搜索网页上 table 中的某些特定值。最终我可以使用一个名为 Alert_Red
的函数获取值。我需要每 5 秒自动调用一次该函数,但是当我尝试下面的代码时,它会给我正确的值,但在无限循环中。
[...some code above...]
def Alert_Red():
if advColor == avc_red and delta <= fivesec_datetime:
print('New RED advisory please check the following link: ', link)
if advColor1 == avc_red and adv_datetime1 == adv_datetime:
print('Another new RED advisory please check the following link: ', link)
schedule.every(5).seconds.do(Alert_Red)
while True:
schedule.run_pending()
time.sleep(1)
第一个“if 子句”为 true 的输出:
New RED advisory please check the following link: , link
New RED advisory please check the following link: , link
New RED advisory please check the following link: , link
[endless loop every 5 sec]
第二个“if 子句”为真时的输出:
Another new RED advisory please check the following link: , link
Another new RED advisory please check the following link: , link
Another new RED advisory please check the following link: , link
[endless loop every 5 sec]
程序必须 运行 连续(即使 if 子句为真,因为我需要始终检查它们)但我只需要被告知一次(第一次找到值 and/or 如果他们改变了),我需要的输出是:
New RED advisory please check the following link: ', link
或第二个“if
子句”为真:
Another new RED advisory please check the following link: ', link
如果我理解你的问题,它可以通过跟踪你是否发布了咨询的全局变量来解决。在此函数之外(在您的启动代码中的某处)将此变量初始化为 False。
def Alert_Red():
global advised
if advColor == avc_red and delta <= fivesec_datetime:
if not advised:
print('New RED advisory please check the following link:', link)
advised = True
if advColor1 == avc_red and adv_datetime1 == adv_datetime:
if not advised:
print('Another new RED advisory please check the following link: ', link)
advised = True
我正在尝试在 Python 中编写一个小程序来搜索网页上 table 中的某些特定值。最终我可以使用一个名为 Alert_Red
的函数获取值。我需要每 5 秒自动调用一次该函数,但是当我尝试下面的代码时,它会给我正确的值,但在无限循环中。
[...some code above...]
def Alert_Red():
if advColor == avc_red and delta <= fivesec_datetime:
print('New RED advisory please check the following link: ', link)
if advColor1 == avc_red and adv_datetime1 == adv_datetime:
print('Another new RED advisory please check the following link: ', link)
schedule.every(5).seconds.do(Alert_Red)
while True:
schedule.run_pending()
time.sleep(1)
第一个“if 子句”为 true 的输出:
New RED advisory please check the following link: , link
New RED advisory please check the following link: , link
New RED advisory please check the following link: , link
[endless loop every 5 sec]
第二个“if 子句”为真时的输出:
Another new RED advisory please check the following link: , link
Another new RED advisory please check the following link: , link
Another new RED advisory please check the following link: , link
[endless loop every 5 sec]
程序必须 运行 连续(即使 if 子句为真,因为我需要始终检查它们)但我只需要被告知一次(第一次找到值 and/or 如果他们改变了),我需要的输出是:
New RED advisory please check the following link: ', link
或第二个“if
子句”为真:
Another new RED advisory please check the following link: ', link
如果我理解你的问题,它可以通过跟踪你是否发布了咨询的全局变量来解决。在此函数之外(在您的启动代码中的某处)将此变量初始化为 False。
def Alert_Red():
global advised
if advColor == avc_red and delta <= fivesec_datetime:
if not advised:
print('New RED advisory please check the following link:', link)
advised = True
if advColor1 == avc_red and adv_datetime1 == adv_datetime:
if not advised:
print('Another new RED advisory please check the following link: ', link)
advised = True