Your four_hour
function may be raising an exception before threading.Timer().start()
is called again:
import threading
import logging
logger = logging.getLogger(__name__)
def five_min():
t = threading.Timer(5, five_min).start()
logger.info("5 min")
def four_hour():
1/0 # <-- Raise an exception
threading.Timer(4, four_hour).start()
logger.info("4 h")
logging.basicConfig(level=logging.DEBUG,
format='[%(asctime)s %(threadName)s] %(message)s',
datefmt='%M:%S')
five_min()
four_hour()
yields
[57:54 MainThread] 5 min
Traceback (most recent call last):
File "script.py", line 21, in <module>
four_hour()
File "script.py", line 12, in four_hour
1/0
ZeroDivisionError: integer division or modulo by zero
[57:59 Thread-1] 5 min
[58:04 Thread-2] 5 min
...
(Note: I changed the delays so the script is easier to test. Once you are
satisfied with the script's qualitative behaviour, you can the delays as you
desire.)
Note: as clemtoy points out, as
long as there is no need for inter-thread or inter-process communication, it may
be easier to use
cron to call
separate scripts which run the five_min
and four_hour
functions. If you do
need inter-process communication, it may still be better to use cron, but you'd
have to structure your code differently, perhaps reading from a database to
learn the state of erstwhile global variables.