我尝试使用Schedule模块在一周中的特定日期和特定时间(基本上是工作时间)运行任务。while循环将在条件为true时工作,但一旦为false,它将不会再次运行以查看时间框架是否仍然适用。我必须重新启动脚本才能工作。你知道吗
我对Python很陌生,自学成才。这是我第一次在这里张贴,所以请纠正我,如果我已经格式化这个不好或没有包括足够的信息。我试着在论坛上搜索这个问题,但是没有找到答案;我可能搜索错了。这也许是个简单的问题,但我已经绞尽脑汁好几天了。这是我试图用于另一个脚本的代码的一小部分测试。其他人推荐Cron,但由于这是一个更大的脚本,我只需要它的一部分运行在一个任务,而不是整个事情。你知道吗
from datetime import datetime as dt
import schedule
import time
def weekdayJob(x, i=None):
week = dt.today().weekday()
if i is not None and week < 5 and dt.now().hour in range(9,17):
schedule.every(i).minutes.do(x)
def printJob():
timestamp = time.strftime("%Y%m%d-%H%M")
print(f"test {timestamp}\n")
weekdayJob(printJob, 5)
while True:
schedule.run_pending()
time.sleep(5)
我也试过了
from datetime import datetime as dt
import schedule
import time
def weekdayJob(x, i=None):
week = dt.today().weekday()
if i is not None and week < 5 and dt.now().hour in range(9,17):
schedule.every(i).minutes.do(x)
def printJob():
timestamp = time.strftime("%Y%m%d-%H%M")
print(f"test {timestamp}\n")
x = 1
while x == 1:
weekdayJob(printJob, 1)
time.sleep(5)
while True:
schedule.run_pending()
time.sleep(5)
我认为将计划的作业放入while循环会使它不断检查条件语句是否为真,但事实似乎并非如此。我如何使它,使这个脚本不断检查,如果平日作业是在所需的时间范围内,所以我不必重新启动脚本。你知道吗
将条件语句移到printjob函数中,不要使用嵌套while循环(这里的代码在工作时间和非工作时间之间切换)。调度任务连续运行,但函数if语句仅在定义的工作时间内运行。这里以1分钟的间隔运行(测试奇数和偶数分钟),您可以指定自己的工作时间:
相关问题 更多 >
编程相关推荐