Python Forum
wanted: python code: sleep to a time period - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: wanted: python code: sleep to a time period (/thread-6095.html)



wanted: python code: sleep to a time period - Skaperen - Nov-06-2017

i would like to have python code, and will eventually write it if no one else has, to sleep until a specified time period, such as a minute.  i have written this in C before.  the period is defined in seconds repeating from the epoch used by the platform time source().  so if the period value is given as 60, it will sleep until the next one minute period.  if the period value is given as 3600, it will sleep until the next one hour period.  so if the current time is 7:59:40, both examples will sleep for 20 seconds until 8:00:00.  if the current time is 7:26:30 the first example (60 second period) will sleep for 30 seconds until 7:27:00 and the second example (3600 second period) will sleep for 2010 seconds until 8:00:00.

the code should be divided up as a command and importable function that does the same thing.  additionally the function should be divided up to have another function that figures out how many seconds to sleep.

an additional feature is offsets.  an offset can also be specified that specifies how many seconds into the period to sleep to.  if the current time is 7:59:40 and the specified period is 60 and the specified offset is 45 then the next 45 second offset of a 60 second period is 7:59:45 (not 8:00:45).  so an offset could result in a shorter sleep time (if the current time has not yet reached the offset in the current period).  an offset of 0 is the same as if offsets are not implemented.

yet another additional feature is minimum time.  this means the time to sleep to is a later period (and offset into that period) so that the sleep time is not less than the minimum.  a minimum of 0 is the same as if minimums are not implemented.

one of the benefits of this way of sleeping is being able to repeat things without accumulating timing errors.  as i mentioned earlier i have already implemented this in C.  that edition was implemented strictly as a command.  you can see that source code here: nsleep.c


RE: wantd: python code: sleep to a time period - metulburr - Nov-06-2017

you can set a time in the future with datetime module as well as add an offset and set a pause until a datetime object of the future with the pause module
pip install pause

then
import pause, datetime
now_plus_10 = datetime.datetime.now() + datetime.timedelta(seconds=10)
pause.until(now_plus_10)
print('over')
then instead of datetime.datetime.now() just insert your own custom time
datetime.datetime(2017, 11, 5, 22, 58, 34, 383752)
at my current time, it would wait 2 hours

But pause has its own functions. pause.days(2) for example.

Then there is always sched
https://docs.python.org/3/library/sched.html

schedule
https://pypi.python.org/pypi/schedule

advanced scheduler
http://apscheduler.readthedocs.io/en/latest/userguide.html

and numerous others like celery


RE: wanted: python code: sleep to a time period - Skaperen - Nov-06-2017

sounds like good suggestions for whoever codes this