how to setup a timer in Python?

Viewed 33

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()


2 Answers

use time.sleep()

import time

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

print('finish after 12 hours')

You just need convert if..else to while and define the function Please to refer to thislink

Related