user2506445
user2506445

Reputation: 129

End a loop when current time is something specific

I have asked a question similar to this before, but this time it is a little different. To me, the following code should work.

import datetime
# run infinitly
while(True):

  done = False

  while(not done):
    #
    #main program
    #


    #stopping condition
      if currenttime == '103000':
        done = True

  #continue with rest of program

However, it doesn't continue with the rest of the program when it hits 10:30:00am.

The following program I KNOW works (on a raspberry pi):

import datetime
done = False
while not done:
    currenttime = datetime.datetime.now().strftime('%H%M%S')
    if currenttime != '103000':
        print currenttime
    if currenttime == '103000':
        done = True
print 'It is 10:30:00am, the program is done.'

It made logical sense to me what I did in that first example. Does anyone know why it won't exit that loop and continue with the rest?

Upvotes: 0

Views: 1330

Answers (4)

moooeeeep
moooeeeep

Reputation: 32502

Note that is not guaranteed that your loop has one iteration in each and every available second. The more load is on your system, the larger the likelihood that the loops skips a second, which potentially would be the termination criterion. There are also cases where seconds may be skipped, e.g. due to time synchronization or daylight saving issues.

Instead of a busy waiting loop, you could pre-compute the timedelta in seconds and then sleep for that many seconds.

Advantages:

  • You'll save computation power that other processes on your machine could use instead.
  • It probably increases the lifetime of your hardware.
  • This will also be more energy efficient.

Example:

import datetime
import time
def wait_until_datetime(target_datetime):
    td = target_datetime - datetime.datetime.now()
    seconds_to_sleep = td.total_seconds()
    if seconds_to_sleep > 0:
        time.sleep(seconds_to_sleep)

target_datetime = datetime.datetime(2025, 1, 1)
wait_until_datetime(target_datetime)
print "Happy New Year 2025!"

Note that this may still fail to produce the desired behavior due to arbitrary changes of the systems date and time settings. Probably it would be best to adopt a completely different strategy to execute a specific command at a specific time. Have you considered implementing the desired behavior using a cron job? (You could send a signal to the process and thereby issue it to cancel the loop...)

Upvotes: 2

kirbyfan64sos
kirbyfan64sos

Reputation: 10727

import datetime
done = False
while True:
    currenttime = datetime.datetime.now().strftime('%H%M%S')
    if currenttime >= '103000':
        break
    print currenttime
print 'It is 10:30:00am, the program is done.'

If you can't use break:

import datetime
done = False
while not done:
    currenttime = datetime.datetime.now().strftime('%H%M%S')
    if currenttime >= '103000':
        done = True
    else:
        print currenttime
print 'It is 10:30:00am, the program is done.'

Upvotes: 0

falsetru
falsetru

Reputation: 368954

If the main program takes a long time to run, currenttime could jump from 102958 to 103005. Therefore skipping 103000 entirely.

Upvotes: 3

jh314
jh314

Reputation: 27802

Maybe you need to set currenttime, before you check? Also, the if statement has to execute exactly at 103000 in order for done = True to execute.

while(True):

  done = False

  while(not done):
    #
    #main program
    #

    # need to set current time
    currenttime = datetime.datetime.now().strftime('%H%M%S')

    #stopping condition (use >= instead of just ==)
      if currenttime >= '103000':
        done = True

  #continue with rest of program

Upvotes: 1

Related Questions