APSchenuler嵌入Django
# 调度器:BackgroundScheduler 非等待 :当调度器需要后台运行时使用。
# 调度器:BackgroundScheduler 非等待 from datetime import datetime from pytz import utc import sys from apscheduler.schedulers.background import BackgroundScheduler, BlockingScheduler from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore from sqlalchemy import create_engine from apscheduler.executors.pool import ProcessPoolExecutor def my_job(id): print('Tick! The time is: %s' % datetime.now()) print(id) sched = BackgroundScheduler({ 'apscheduler.jobstores.default': { 'type': 'sqlalchemy', 'url': 'postgres://postgres:123456@127.0.0.1:5432/postgres' }, 'apscheduler.executors.default': { 'class': 'apscheduler.executors.pool:ThreadPoolExecutor', 'max_workers': '20' }, 'apscheduler.executors.processpool': { 'type': 'processpool', 'max_workers': '5' }, 'apscheduler.job_defaults.coalesce': 'false', 'apscheduler.job_defaults.max_instances': '3', 'apscheduler.timezone': 'Asia/Shanghai', # 设置时区 ,存储桶时区 }) sched.start() # # .. do something else here, maybe add jobs etc. # scheduler.start() # scheduler.add_job(myfunc, run_date=datetime(2020, 3, 24, 10, 13, 5)) # job = sched.add_job(my_job, 'date', run_date=datetime(2020, 3, 24, 14, 30, 5), args=['1234567'], id="1234567", ) # print(job) sched.remove_job('1234567') # 删除任务
# 调度器:BlockingScheduler阻塞等待: 当调度器是程序中唯一运行的东西时使用,阻塞式。
# 调度器:BlockingScheduler阻塞等待 from datetime import datetime from pytz import utc import sys from apscheduler.schedulers.background import BackgroundScheduler,BlockingScheduler from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore from sqlalchemy import create_engine from apscheduler.executors.pool import ProcessPoolExecutor # def my_job(id): # print('Tick! The time is: %s' % datetime.now()) # # print(id) # # # sched = BlockingScheduler({ # 'apscheduler.jobstores.default': { # 'type': 'sqlalchemy', # 'url': 'postgres://postgres:123456@127.0.0.1:5432/postgres' # }, # 'apscheduler.executors.default': { # 'class': 'apscheduler.executors.pool:ThreadPoolExecutor', # 'max_workers': '20' # # }, # 'apscheduler.executors.processpool': { # 'type': 'processpool', # 'max_workers': '5' # }, # 'apscheduler.job_defaults.coalesce': 'false', # 'apscheduler.job_defaults.max_instances': '3', # 'apscheduler.timezone': 'Asia/Shanghai', #设置时区 ,存储桶时区 # }) # # # # .. do something else here, maybe add jobs etc. # # # scheduler.start() # # scheduler.add_job(myfunc, run_date=datetime(2020, 3, 24, 10, 13, 5)) # job = sched.add_job(my_job, 'date', run_date=datetime(2020, 3, 24, 14, 15, 5), args=['1234567'],id="1234567",) # print(job) # # sched.add_job(my_job) # # # sched.start()