0

I need to set multi-timers in the same script, i have two functions fun_1() and fun_2() that I want to execute one time by minute.

I want to execute the two timers in the same time and be able to kill them with keyboardinterrupt.

What I tried so far is:

import threading 

def timer_1():
    fun_1()
    threading.Timer(60.0, timer_1).start()
def timer_2():
    fun_2()
    threading.Timer(60.0, timer_2).start()

if __name__ == "__main__":
    timer_1()
    timer_2()

There is two problems with my code:

  1. timer_2 will be executed after one minute of the timer_1 execution
  2. The keyboardInterruptis ignored
farhawa
  • 10,120
  • 16
  • 49
  • 91
  • Doesn't look like threading was implemented properly, but having each timer on a separate thread seems like a good approach. For basics of https://www.toptal.com/python/beginners-guide-to-concurrency-and-parallelism-in-python – oortCloud Mar 14 '16 at 23:12
  • The timer itself is a separated thread, do you think it's a good idea to run separated threads in separated threads ? – farhawa Mar 14 '16 at 23:15
  • The keyboard interrupt isn't being handled because you didn't register a handler. See http://stackoverflow.com/questions/4205317/capture-keyboardinterrupt-in-python-without-try-except . – Garrett Hyde Mar 15 '16 at 00:11

0 Answers0