Don't use unaware local datetime objects such as returned by now()
unless you want to show them to a local user. There are many perils.
There is a difference between "What time is it?" and "How many seconds elapsed?". The former is easier to find out.
To run for N
hours, you could:
from time import monotonic as timer # or time.time if it is not available
endtime = timer() + N * 3600
while timer() < endtime:
# do your thing (mind what clocks use sleep(), join(), lock.acquire())
It works even if computer time has changed during the program execution manually or due to DST transition. See Rational
section from pep-418 for introducing time.monotonic
in Python.
You can choose other timers depending on your needs/available systems. For example, you could use a timer that provides better precision but might overflow sooner or that takes into account NTP adjacements that might provide better clock than your local CPU or the time while the system were asleep or suspended (imagine what do you want to happen after you open the cover of your notebook after several hours).
datetime.now()
object (naive broken-down time) might be useful if you want something happen at some local time regardless of how many seconds passed since now e.g., to do something after 7pm
whatever day (before midnight):
from datetime import datetime, time
if datetime.now().time() > time(19):
# do something