Reputation: 123
I would like to ask: how to write a python program to calculate 1 hour has been passed since we started a python program?
I take an example: - We started the python program at 17:00:00 - After running 1 hour (at 18:00:00), python program will print a message to inform 1 hour has been passed.
I can figure out a python program. Firstly, I record the time at starting the program (called start_time
), then I continuously record the next time (called end_time
). If the (end_time
- start_time
== 1 hour), it prints a message.
However it seems the program wastes so much CPU performance! I need a program that take less CPU performance.
EDIT: I need as below.
I have a thread name wait_an_event_thread
. It is a blocking thread. If event trigger
is not set, this thread is blocked.
During blocking time, if 1 hour has been passed, this thread print out a message.
That is my expectation.
Previously, I said that I continuously record next time (call end_time
). It meant I intended change from blocking thread to non-blocking thread because I did not know how to print a message in blocked thread if 1 hour has been passed. But it seems non-blocking thread take so much CPU performance.
This is my code:
MY EXPECTATION: blocking thread
def wait_an_event_thread(trigger):
trigger.wait()
# If 1 hour has been passed, it print a message
# How to print message if 1 hour has been passed in this blocking thread if the "trigger" event is not set??
trigger = threading.Event()
wait_an_event_thread = threading.Thread(name='wait_an_event_thread',
target=wait_an_event_thread,
args=(trigger,))
wait_an_event_thread.start()
Upvotes: 1
Views: 1423
Reputation: 52949
As it turned out you needed to use the timeout parameter of Event.wait()
and check if the event has been set upon return or not:
def wait_an_event_thread(trigger):
while not trigger.wait(3600):
print "1 hour has passed"
# Do what must be done when triggered
trigger = threading.Event()
the_thread = threading.Thread(name='wait_an_event_thread',
target=wait_an_event_thread,
args=(trigger,))
the_thread.start()
This'll keep on printing the diagnostic message between hour(ish) intervals, if the Event is not set. Event.wait()
might behave badly if your system clock jumps backwards.
Upvotes: 1
Reputation: 1856
import threading
def func():
#your code here
t = threading.Timer(3600, func)
t.start()
Thanks to @Ilja Everilä for the corrections and improvements
Upvotes: 4
Reputation: 403
start a thread which will call a function after every hour and print message. you can write the code like this with your main function: import threading
def print():
print "One Hour has been passed"
t = threading.Timer(3600, print)
t.start()
This will call the print function after every hour and print the message.
Upvotes: 0
Reputation: 33
I suggest after one hour you use
`time.sleep(t)`
where t
being as high as possible without affecting your program.
Upvotes: 0
Reputation: 3865
You could take advantage of multi-threading and create a separate thread that runs separately from the rest of the code. That thread simply sleeps for 1h and then prints "1h have been passed" then sleeps again for another hour.
To put it in code form, you will need something like this:
import time
def print_message_every_interval( threadName, message, interval=3600, ):
while True:
time.sleep(interval) #interval in seconds
print ("%s: %s: %s" % ( threadName, time.ctime(time.time()), message ))
and in your main() add something like:
import _thread
if __name__=="__main__":
#
# your init code here
#
try:
_thread.start_new_thread( print_message_every_interval, \
("Timer-1h", "One hour have passed", 3600 ) )
except Exception as e:
print('could not start new thread')
print(e)
#
# the rest of your code here
#
of course there are many multi-threading libraries you can use to make the code look fancy. Like the threading
module.
You can find more on this topic here: https://www.tutorialspoint.com/python3/python_multithreading.htm
Upvotes: 0