使用模块 schedule 运行 schedule immediately then again every hour
Using module schedule run schedule immediately then again every hour
我正在尝试使用模块 "schedule" 每小时安排一个任务。我的问题是我需要任务首先 运行 然后每小时再次 运行。
这段代码工作正常,但它在初始 运行
之前等待一个小时
import schedule
import time
def job():
print("This happens every hour")
schedule.every().hour.do(job)
while True:
schedule.run_pending()
我想避免这样做:
import schedule
import time
def job():
print("This happens immediately then every hour")
schedule.every().hour.do(job)
while i == 0:
job()
i = i+1
while i == 1:
schedule.run_pending()
理想情况下,有这样的选项会很好:
schedule.run_pending_now()
可能 最简单 的解决方案是立即 运行 它并安排它,例如:
import schedule
import time
def job():
print("This happens every hour")
schedule.every().hour.do(job)
job() # Runs now.
while True:
schedule.run_pending() # Runs every hour, starting one hour from now.
对于 运行 所有作业,无论是否计划 运行,请使用 schedule.run_all()
。作业在完成后重新安排,就像使用 run_pending()
.
执行它们一样
def job_1():
print('Foo')
def job_2():
print('Bar')
schedule.every().monday.at("12:40").do(job_1)
schedule.every().tuesday.at("16:40").do(job_2)
schedule.run_all()
# Add the delay_seconds argument to run the jobs with a number
# of seconds delay in between.
schedule.run_all(delay_seconds=10)```
如果您有许多任务需要一些时间来执行,并且您希望在启动期间独立地 运行 它们,您可以使用 threading
import schedule
import time
def job():
print("This happens every hour")
def run_threaded(task):
job_thread = threading.Thread(target=task)
job_thread.start()
run_threaded(job) #runs job once during start
schedule.every().hour.do(run_threaded, job)
while True:
schedule.run_pending() # Runs every hour, starting one hour from now.
我正在尝试使用模块 "schedule" 每小时安排一个任务。我的问题是我需要任务首先 运行 然后每小时再次 运行。
这段代码工作正常,但它在初始 运行
之前等待一个小时import schedule
import time
def job():
print("This happens every hour")
schedule.every().hour.do(job)
while True:
schedule.run_pending()
我想避免这样做:
import schedule
import time
def job():
print("This happens immediately then every hour")
schedule.every().hour.do(job)
while i == 0:
job()
i = i+1
while i == 1:
schedule.run_pending()
理想情况下,有这样的选项会很好:
schedule.run_pending_now()
可能 最简单 的解决方案是立即 运行 它并安排它,例如:
import schedule
import time
def job():
print("This happens every hour")
schedule.every().hour.do(job)
job() # Runs now.
while True:
schedule.run_pending() # Runs every hour, starting one hour from now.
对于 运行 所有作业,无论是否计划 运行,请使用 schedule.run_all()
。作业在完成后重新安排,就像使用 run_pending()
.
def job_1():
print('Foo')
def job_2():
print('Bar')
schedule.every().monday.at("12:40").do(job_1)
schedule.every().tuesday.at("16:40").do(job_2)
schedule.run_all()
# Add the delay_seconds argument to run the jobs with a number
# of seconds delay in between.
schedule.run_all(delay_seconds=10)```
如果您有许多任务需要一些时间来执行,并且您希望在启动期间独立地 运行 它们,您可以使用 threading
import schedule
import time
def job():
print("This happens every hour")
def run_threaded(task):
job_thread = threading.Thread(target=task)
job_thread.start()
run_threaded(job) #runs job once during start
schedule.every().hour.do(run_threaded, job)
while True:
schedule.run_pending() # Runs every hour, starting one hour from now.