Python Threading timer not working across suspend
Question: 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?


Answer: 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()
    

