将sched.add_job与具有相同时间和日期触发器的两个不同作业一起运行吗? 如果没有,如何使用相同的后台调度程序添加具有不同触发时间的第二个作业?
答案 0 :(得分:1)
您可以使用apscheduler同时执行两个作业:
from flask import Flask
import atexit
from apscheduler.schedulers.background import BackgroundScheduler
scheduler = BackgroundScheduler()
def job1():
for _ in range(1000):
print('a')
def job2():
for _ in range(1000):
print('b')
h = 10
m = 0
# set h,m to the time you are ready to test
scheduler.add_job(job1,'cron',hour=h,minute=m)
scheduler.add_job(job2,'cron',hour=h,minute=m)
scheduler.start()
atexit.register(lambda: scheduler.shutdown())
app = Flask(__name__)
if __name__ == "__main__":
app.run()
答案 1 :(得分:0)
from apscheduler.schedulers.background import BackgroundScheduler
def my_task1():
print ("Task 1")
def my_task2():
print ("Task 2")
if __name__ == '__main__':
process_scheduler = BackgroundScheduler()
process_scheduler.add_job(my_task1, 'cron', id='my_task1', seconds=5)
process_scheduler.add_job(my_task2, 'cron', id='my_task2', seconds=10)
process_scheduler.start()
atexit.register(lambda: process_scheduler.shutdown())