Trigger a Python function exactly on the minute

user2929831 picture user2929831 · Oct 28, 2013 · Viewed 14.9k times · Source

I have a function that I want to trigger at every turn of the minute — at 00 seconds. It fires off a packet over the air to a dumb display that will be mounted on the wall.

I know I can brute force it with a while loop but that seems a bit harsh.

I have tried using sched but that ends up adding a second every minute.

What are my options?

Answer

Christian Ternus picture Christian Ternus · Oct 28, 2013

You might try APScheduler, a cron-style scheduler module for Python.

From their examples:

from apscheduler.scheduler import Scheduler

# Start the scheduler
sched = Scheduler()
sched.start()

def job_function():
    print "Hello World"

sched.add_cron_job(job_function, second=0)

will run job_function every minute.