Search code examples
pythonfunctionunixtimestampunix-timestamp

How do i run a python function at a certain timestamp time. Without outside software?


I want to tell a python script to run a certain function when a certain timestamp time happens

I have already looked for running time specific functions but i could not find anything that answered this specific question about counting time stamps

#input is number of days till due date
dueDate = int(input('Days until it is due: '))

#86400 seconds in a day
days = dueDate * 86400

#gets current time stamp time 
currentT = int(time.time())

#gets the timestamp for due date 
alarm = days+currentT

the aim is to find the python function which can run another function from within the script at when a specified future time stamp occurs


Solution

  • Schedule is a good python module for doing this.

    Usage: (From Documentation)

    Install

    $ pip install schedule
    

    Usage

    import schedule
    import time
    
    def job():
        print("I'm working...")
    
    schedule.every(10).minutes.do(job)
    schedule.every().hour.do(job)
    schedule.every().day.at("10:30").do(job)
    schedule.every(5).to(10).minutes.do(job)
    schedule.every().monday.do(job)
    schedule.every().wednesday.at("13:15").do(job)
    schedule.every().minute.at(":17").do(job)
    
    while True:
        schedule.run_pending()
        time.sleep(1)