变身曲之都市红颜笑,醋泡黑豆的作用,风云之霸王枪
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()
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
新手学习Python2和Python3中print不同的用法
Python基于os.environ从windows获取环境变量
网友评论