Search code examples
pythonschedule

Python schedule run with inconsistence


I've a main function with some code and i need run this every a predetermined time, but independent of the time that i configure it run every 2-3 minutes. I don't have an idea what's going. I'm show some example below.

import schedule

def main():
    print('Some code here...')
    schedule.run_pending()

# the function main should be run every 30min...?
schedule.every(30).minutes.do(main)
schedule.every().hour.do(main)

main()

For what i researched this code shoud run every 30 minutes, but it run every 2-3 minutes.


Solution

  • You should not call your scheduled function directly. In your desired scenario, the function should run every X minutes - Meaning that the script that is responsible for running it, should run all the time, deciding when to invoke the function. a while True should do.

    import schedule
    
    def main():
        print('Some code here...')
    
    # the function main should be run every 30min...?
    schedule.every(30).minutes.do(main)
    schedule.every().hour.do(main)
    
    while True:
        schedule.run_pending()