Python Forum
wanted: python code: sleep to a time period
Thread Rating:
  • 2 Vote(s) - 1.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
wanted: python code: sleep to a time period
#1
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
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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/lat...guide.html

and numerous others like celery
Recommended Tutorials:
Reply
#3
sounds like good suggestions for whoever codes this
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  sleep to half past the hour Skaperen 11 3,952 Oct-29-2020, 06:41 AM
Last Post: Skaperen
  my ideas for a new sleep command Skaperen 1 1,954 Sep-02-2019, 04:17 AM
Last Post: Larz60+
  [Python Developer Wanted] ‘Women in Tech’ Worldwide Training Initiative from Oxford vanicci 2 2,844 Jun-27-2018, 04:25 PM
Last Post: vanicci
  Command wanted in Python: mgrep Skaperen 1 2,825 Mar-13-2018, 07:38 AM
Last Post: Gribouillis
  program/scrpt wanted (in python, of course) Skaperen 5 4,421 Oct-23-2017, 03:25 PM
Last Post: snippsat
  want command program: sleep to a specific time Skaperen 3 3,567 Jul-11-2017, 01:33 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020