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:
JavaScript
x
8
1
if time == 12 hours:
2
finish
3
else:
4
if 1 hours passed:
5
methodFoo()
6
7
8
Advertisement
Answer
use time.sleep()
JavaScript
1
10
10
1
import time
2
3
hours = 0
4
while hours < 12:
5
methodFoo()
6
hours += 1
7
time.sleep(3600) # seconds
8
9
print('finish after 12 hours')
10