Before I ask, Cron Jobs and Task Scheduler will be my last options, this script will be used across Windows and Linux and I'd prefer to have a coded out method of doing this than leaving this to the end user to complete.
Is there a library for Python that I can use to schedule tasks? I will need to run a function once every hour, however, over time if I run a script once every hour and use .sleep, "once every hour" will run at a different part of the hour from the previous day due to the delay inherent to executing/running the script and/or function.
What is the best way to schedule a function to run at a specific time of day (more than once) without using a Cron Job or scheduling it with Task Scheduler?
Or if this is not possible, I would like your input as well.
import datetime import time from apscheduler.scheduler import Scheduler  # Start the scheduler sched = Scheduler() sched.daemonic = False sched.start()  def job_function():     print("Hello World")     print(datetime.datetime.now())     time.sleep(20)  # Schedules job_function to be run once each minute sched.add_cron_job(job_function,  minute='0-59')  out:
>Hello World >2014-03-28 09:44:00.016.492 >Hello World >2014-03-28 09:45:00.0.14110  (From Animesh Pandey's answer below)
from 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() 
                One good approach is to schedule these scripts as per your need; daily at 5 PM or weekly once on Sunday 12 AM etc. There are a number tools available at your disposal such as — schedule, apscheduler, python-crontab, apache-airflow, etc. that you can use to schedule your Python jobs .
In order to schedule the Python script using the Windows Scheduler: Open the Windows Control Panel and then click on the Administrative Tools. Double-click on the Task Scheduler, and then choose the option to 'Create Basic Task…' Type a name for your task (you can also type a description if needed), and then press Next ...
With the help of the Schedule module, we can make a python script that will be executed in every given particular time interval. with this function schedule. every(5). minutes.do(func) function will call every 5 minutes.
Maybe this can help: Advanced Python Scheduler
Here's a small piece of code from their documentation:
from apscheduler.schedulers.blocking import BlockingScheduler  def 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, timedelta  while 1:     print 'Run something..'      dt = datetime.now() + timedelta(hours=1)     dt = dt.replace(minute=10)      while datetime.now() < dt:         time.sleep(1) 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With