Scheduling Python Script to run every hour accurately
Maybe this can help: Advanced Python Scheduler
Here's a small piece of code from their documentation:
from apscheduler.schedulers.blocking import BlockingSchedulerdef some_job(): print "Decorated job"scheduler = BlockingScheduler()scheduler.add_job(some_job, 'interval', hours=1)scheduler.start()
To run something every 10 minutes past the hour.
from datetime import datetime, timedeltawhile 1: print 'Run something..' dt = datetime.now() + timedelta(hours=1) dt = dt.replace(minute=10) while datetime.now() < dt: time.sleep(1)
For apscheduler
< 3.0, see Unknown's answer.
For apscheduler
> 3.0
from apscheduler.schedulers.blocking import BlockingSchedulersched = 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()
Update:
apscheduler
documentation.
This for apscheduler-3.3.1
on 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 utcfrom apscheduler.schedulers.blocking import BlockingSchedulerfrom apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStorefrom 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 Method1sched_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()