Skip to content
Advertisement

how to setup a timer in Python?

I would like to create a timer in Python. After I run the .py class I would like a method to be called every hour in 12 hour period, after 12 hours end the program closes. How can I do this?

I want something like:

    if time == 12 hours:
    finish
    else:
    if 1 hours passed:
    methodFoo()


Advertisement

Answer

use time.sleep()

import time

hours = 0
while hours < 12:
    methodFoo()
    hours += 1
    time.sleep(3600) # seconds

print('finish after 12 hours')
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement