python运行时间表_精确地安排Python脚本每小时运行一次

有关apscheduler<3.0,请参见Unknown's answer。

对于apscheduler>3.0from apscheduler.schedulers.blocking import BlockingScheduler

sched = BlockingScheduler()

@sched.scheduled_job('interval', seconds=10)

def timed_job():

print('This job is run every 10 seconds.')

@sched.scheduled_job('cron', day_of_week='mon-fri', hour=10)

def scheduled_job():

print('This job is run every weekday at 10am.')

sched.configure(options_from_ini_file)

sched.start()

更新:

这是给apscheduler-3.3.1的Python 3.6.2。"""

Following configurations are set for the scheduler:

- a MongoDBJobStore named “mongo”

- an SQLAlchemyJobStore named “default” (using SQLite)

- a ThreadPoolExecutor named “default”, with a worker count of 20

- a ProcessPoolExecutor named “processpool”, with a worker count of 5

- UTC as the scheduler’s timezone

- coalescing turned off for new jobs by default

- a default maximum instance limit of 3 for new jobs

"""

from pytz import utc

from apscheduler.schedulers.blocking import BlockingScheduler

from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore

from apscheduler.executors.pool import ProcessPoolExecutor

"""

Method 1:

"""

jobstores = {

'mongo': {'type': 'mongodb'},

'default': SQLAlchemyJobStore(url='sqlite:///jobs.sqlite')

}

executors = {

'default': {'type': 'threadpool', 'max_workers': 20},

'processpool': ProcessPoolExecutor(max_workers=5)

}

job_defaults = {

'coalesce': False,

'max_instances': 3

}

"""

Method 2 (ini format):

"""

gconfig = {

'apscheduler.jobstores.mongo': {

'type': 'mongodb'

},

'apscheduler.jobstores.default': {

'type': 'sqlalchemy',

'url': 'sqlite:///jobs.sqlite'

},

'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': 'UTC',

}

sched_method1 = BlockingScheduler() # uses overrides from Method1

sched_method2 = BlockingScheduler() # uses same overrides from Method2 but in an ini format

@sched_method1.scheduled_job('interval', seconds=10)

def timed_job():

print('This job is run every 10 seconds.')

@sched_method2.scheduled_job('cron', day_of_week='mon-fri', hour=10)

def scheduled_job():

print('This job is run every weekday at 10am.')

sched_method1.configure(jobstores=jobstores, executors=executors, job_defaults=job_defaults, timezone=utc)

sched_method1.start()

sched_method2.configure(gconfig=gconfig)

sched_method2.start()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值