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