3

I'm trying to create a program that syncs with a server every 60 seconds. The code I'm using to do that looks like:

threading.Timer(60, self.sync, [""]).start()

Pretty simple, and works great. The issue is if I decide to suspend the machine for a period of time and then come back, it doesn't work anymore. It's as if the timer stopped working. I suspect this has to do with the gap in real-world time as perceived by the timer, but I'm not sure how to make it work.

The app I'm making is targeted at OSX, so maybe there is a system-level timer that I could have access to?

Jack Slingerland
  • 2,651
  • 5
  • 34
  • 56
  • This probably just does the same thing under-the-hood, but you could try the method here instead: http://stackoverflow.com/a/4153314/429982 – Gerrat May 29 '14 at 12:23
  • possible duplicate: http://stackoverflow.com/questions/2398661/schedule-a-repeating-event-in-python-3 – johntellsall Aug 19 '14 at 20:19

1 Answers1

0

I don't know if this would work any better, but you could give it a try:

import time

def call_sync(freq, meth):
    cur_time = time.time()
    while True:
        while (time.time() - cur_time) < freq:
            yield None
        meth()
        cur_time = time.time()


def sync():
    print("synced")

def main():
    timer = call_sync(60, sync)
    while True:
        time.sleep(1)
        next(timer)

if __name__ == '__main__':
    main()
Gerrat
  • 28,863
  • 9
  • 73
  • 101