标签:guide tin mongodb mamicode module executors 结束 ima get
1 pip install apscheduler
首先来看一个周一到周五每天早上6点半喊我起床的例子:
1 from apscheduler.schedulers.blocking import BlockingScheduler 2 from datetime import datetime 3 # 输出时间 4 def job(): 5 print(datetime.now().strftime("%Y-%m-%d %H:%M:%S")) 6 # BlockingScheduler 7 scheduler = BlockingScheduler() 8 scheduler.add_job(job, ‘cron‘, day_of_week=‘1-5‘, hour=6, minute=30) 9 scheduler.start()
1 from apscheduler.schedulers.blocking import BlockingScheduler 2 from datetime import datetime 3 4 5 def job(): 6 print(datetime.now().strftime("%Y-%m-%d %H:%M:%S")) 7 # 定义BlockingScheduler 8 sched = BlockingScheduler() 9 sched.add_job(job, ‘interval‘, seconds=5) 10 sched.start()
1 from datetime import datetime 2 from pymongo import MongoClient 3 from apscheduler.schedulers.blocking import BlockingScheduler 4 from apscheduler.jobstores.memory import MemoryJobStore 5 from apscheduler.jobstores.mongodb import MongoDBJobStore 6 from apscheduler.executors.pool import ThreadPoolExecutor, ProcessPoolExecutor 7 # MongoDB 参数 8 host = ‘127.0.0.1‘ 9 port = 27017 10 client = MongoClient(host, port) 11 # 输出时间 12 def job(): 13 print(datetime.now().strftime("%Y-%m-%d %H:%M:%S")) 14 # 存储方式 15 jobstores = { 16 ‘mongo‘: MongoDBJobStore(collection=‘job‘, database=‘test‘, client=client), 17 ‘default‘: MemoryJobStore() 18 } 19 executors = { 20 ‘default‘: ThreadPoolExecutor(10), 21 ‘processpool‘: ProcessPoolExecutor(3) 22 } 23 job_defaults = { 24 ‘coalesce‘: False, 25 ‘max_instances‘: 3 26 } 27 scheduler = BlockingScheduler(jobstores=jobstores, executors=executors, job_defaults=job_defaults) 28 scheduler.add_job(job, ‘interval‘, seconds=5, jobstore=‘mongo‘) 29 scheduler.start()
1 from apscheduler.schedulers.blocking import BlockingScheduler 2 sched = BlockingScheduler() 3 # 装饰器 4 @sched.scheduled_job(‘interval‘, id=‘my_job_id‘, seconds=5) 5 def job_function(): 6 print("Hello World") 7 # 开始 8 sched.start()
1 job = scheduler.add_job(myfunc, ‘interval‘, minutes=2) 2 job.remove() 3 # id 4 scheduler.add_job(myfunc, ‘interval‘, minutes=2, id=‘my_job_id‘) 5 scheduler.remove_job(‘my_job_id‘)
1 apscheduler.job.Job.pause() 2 apscheduler.schedulers.base.BaseScheduler.pause_job()
恢复一个 job:
1 apscheduler.job.Job.resume() 2 apscheduler.schedulers.base.BaseScheduler.resume_job()
希望你还记得 apscheduler.job.Job 是 add_job() 返回的实例
1 job.modify(max_instances=6, name=‘Alternate name‘) 2 modify_job(‘my_job_id‘, trigger=‘cron‘, minute=‘*/5‘)
1 scheduler.shutdown() 2 scheduler.shutdown(wait=False)
1 def my_listener(event): 2 if event.exception: 3 print(‘The job crashed :(‘) 4 else: 5 print(‘The job worked :)‘) 6 # 添加监听器 7 scheduler.add_listener(my_listener, EVENT_JOB_EXECUTED | EVENT_JOB_ERROR)
1 from datetime import date 2 from apscheduler.schedulers.blocking import BlockingScheduler 3 sched = BlockingScheduler() 4 def my_job(text): 5 print(text) 6 # The job will be executed on November 6th, 2009 7 sched.add_job(my_job, ‘date‘, run_date=date(2009, 11, 6), args=[‘text‘]) 8 sched.add_job(my_job, ‘date‘, run_date=datetime(2009, 11, 6, 16, 30, 5), args=[‘text‘]) 9 sched.add_job(my_job, ‘date‘, run_date=‘2009-11-06 16:30:05‘, args=[‘text‘]) 10 # The ‘date‘ trigger and datetime.now() as run_date are implicit 11 sched.add_job(my_job, args=[‘text‘]) 12 sched.start()
中文释义:
参数
|
说明
|
(int|str)
|
表示参数既可以是int类型,也可以是str类型
|
(datetime | str)
|
表示参数既可以是datetime类型,也可以是str类型
|
year(int or str)
|
年,4位数字
|
month(int or str)
|
月(范围1-12)
|
day(int or str)
|
日(范围1-31)
|
week(int or str)
|
周(范围1-53)
|
day_of_week(int or str)
|
周内第几天或者星期几(范围0-6或者mon,tue,wed,thu,fri,stat,sun)
|
hour(int or str)
|
时(0-23)
|
minute(int or str)
|
分(0-59)
|
second(int or str)
|
秒(0-59)
|
start_date(datetime or str)
|
最早开始日期(含)
|
end_date(datetime or str)
|
最晚结束日期(含)
|
timezone(datetime.tzinfo or str) | 指定时区 |
表达式:
示例:
1 from apscheduler.schedulers.blocking import BlockingScheduler 2 3 4 def job_function(): 5 print("Hello World") 6 # BlockingScheduler 7 sched = BlockingScheduler() 8 # Schedules job_function to be run on the third Friday 9 # of June, July, August, November and December at 00:00, 01:00, 02:00 and 03:00 10 sched.add_job(job_function, ‘cron‘, month=‘6-8,11-12‘, day=‘3rd fri‘, hour=‘0-3‘) 11 # Runs from Monday to Friday at 5:30 (am) until 2014-05-30 00:00:00 12 sched.add_job(job_function, ‘cron‘, day_of_week=‘mon-fri‘, hour=5, minute=30, end_date=‘2014-05-30‘) 13 sched.start()
1 from datetime import datetime 2 from apscheduler.schedulers.blocking import BlockingScheduler 3 4 5 def job_function(): 6 print("Hello World") 7 # BlockingScheduler 8 sched = BlockingScheduler() 9 # Schedule job_function to be called every two hours 10 sched.add_job(job_function, ‘interval‘, hours=2) 11 # The same as before, but starts on 2010-10-10 at 9:30 and stops on 2014-06-15 at 11:00 12 sched.add_job(job_function, ‘interval‘, hours=2, start_date=‘2010-10-10 09:30:00‘, end_date=‘2014-06-15 11:00:00‘) 13 sched.start()
标签:guide tin mongodb mamicode module executors 结束 ima get
原文地址:https://www.cnblogs.com/longweiqiang/p/11993929.html