Skip to content
Advertisement

Call a Function After a Set Time But Continue to Run Code the Program with Python

I’m wanting to call a function after a set time, but continue to run the python program. Is this possible?

Example use case:

With the following code, I want to call the function happy_birthday after 2 days but continually print Not your birthday until then.

JavaScript

Advertisement

Answer

Your are looking for threading.Timer.
This calls after a specific time a method as thread.

I assume you want to exit the program with exit() in happy_birthday? You can do this by setting the condition (the variable run) to False.

JavaScript

Output

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement