Search code examples
python-3.xapache-kafkatimerpython-multithreading

Calling function after every 24hrs without affecting following code


from threading import Timer    

def Add():
  ...Some process...
  Timer(86400, Add).start() ## 86400 secs in 24 hours.

if __name__ == '__main__':
  Add()

  "Consuming Kafka Messages and will run continously once started".

Once the code runs Add function is called and program starts to consume kafka messages and waits for it continously. I want to call the "Add" function every 24 hrs without disturbing kafka process. For this I have tried one threading.timer but I am not sure will it work or not.

I have one more question- is this thread initialize every 24hrs or it one thread just calls every 24 hrs.

Please advice I am doing correct and will it work or not and thanks in advance!!


Solution

  • You can simply use an extra function that calls add() and adds the timer for you, something like this:

    def task1():
        while True:
            add()
            time.sleep(86400)
    if __name__ == "__main__":
        thread = Thread(target=task1)
        thread.start()
        # add kafka code