我正在尝试使用计划模块在一周的特定日期和特定时间(基本上是工作时间)运行任务。当条件为true时,while循环将起作用,但条件为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循环中将使它不断检查条件语句是否为真,但事实并非如此。我将如何做到这一点,以便该脚本不断检查weekdayJob是否在所需的时间范围内,因此我不必重新启动脚本。
我正在尝试使用计划模块在一周的特定日期和特定时间(基本上是工作时间)运行任务。当条件为true时,while循环将起作用,但是一旦满足条件,就可以使用...
import datetime as dt
import schedule, time
from datetime import datetime
def printJob():
week = dt.datetime.today().weekday()
timestamp = time.strftime("%Y%m%d-%H%M")
if week < 5 and datetime.now().minute % 2 ==0: # in range(9,17):
print(f"business hours test...EVEN minutes... {timestamp}\n")
# your business hours function here...
else:
print(f'go home...test... ODD minutes... {timestamp}\n')
schedule.every(1).minute.do(printJob)
while True:
schedule.run_pending()