Skip to content Skip to sidebar Skip to footer

Time As Stopping Condition Of Infinite Loop

# run infinitly while(True): done = False; while(not done): #Do Stuff #Main Program #stopping condition of inner while loop if datetime.datetime.now().minute

Solution 1:

There are various ways to do it, but keeping close to your original design, I'd code the inner loop as:

while(not done):
    if datetime.datetime.now().minute % 10 == 0:
        done = True
    else:
        time.sleep(60-datetime.datetime.now().second)

(I'm assuming what you were trying to do with that second if was sleep until the next minute when you weren't ready to exit the loop.)

Solution 2:

It stops every tenth minute: 1:10, 2:10, 3:10, etc. In order to do that, use something like:

import time

# in outer loop
inner_loop_start = time.time()

# in inner loop
    now = time.time()
    if now - inner_loop_start > 60*10:
        # done= True and all that jazz

Solution 3:

Rather than thrashing around constantly checking if it's 10 past the top of the hour, just do some math and sleep appropriately. Time is generally deterministic.

import datetime
import time

defseconds_till_10past():
    now = datetime.datetime.now()
    delta = now.replace(minute=10, second=0) - now
    return delta.seconds % 3600whileTrue:
    time.sleep(seconds_till_10past())
    print"Do something at 10 past the hour."

Also, don't parenthesize arguments to statements (e.g. while(True)), it's bad form.

Solution 4:

This does not directly answer the question, but just in case all you want to achieve in the first place is to run some python code every (say) 10 minutes, you'd better implement this using cron.

I assume you so far have a script that is somehow started at boot time. It mainly consists of an infinite loop, a main procedure, and a wait-until-next-execution-time component. For example like the following:

""" my_first_daemon.py
    does something everytime the datetime minute part is fully divisible by ten
"""whileTrue:
    # do something at 00,10,20,30,40,50 (minutes) every hourprint"Hello World!"# wait until the next execution time
    wait_until_next_ten_minute_time()

If this is indeed the case I'd suggest to move the main section of your script to a separate script. For example like the following:

""" my_first_cronjob.py
    is run by cron everytime the datetime minute part is fully divisible by ten
"""# do something at 00,10,20,30,40,50 (minutes) every hourprint"Hello World!"

The you go ahead and add to your crontab (use the command crontab -e to add the entry to that user's crontab that shall run the script, also have a look at the manpage). For example like this:

# Example of job definition:# .---------------- minute (0 - 59)# |  .------------- hour (0 - 23)# |  |  .---------- day of month (1 - 31)# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat# |  |  |  |  |# *  **  **     command to be executed

# example entry:
# (divide minute-star by 10 to get it run every 10 minutes) 
*/10 *  **  *     python /path/to/my_first_cronjob.py

After the edit you should notice a message like crontab: installing new crontab and then you're done. Just wait and have a look if it works.

Some things to note as well:

  • The script's output to stdout and stderr are sent to you by mail after termination. Have a look at tail -f /var/mail/moooeeeep (specify your username). You can also configure Thunderbird to fetch these mails, in order to inspect them easier.
  • The corresponding Wikipedia page is a very good source of information for further details.
  • If you need to keep state information between independent runs of the script, consider using a configuration file to store this state information, e.g., using pickle, shelve, sqlite, whatever.

Solution 5:

Expanding from my comment above:

tic = datetime.datetime.now()

while True:

    # do stuff

    toc = datetime.datetime.now() - tic
    if toc.minute > 10:
        tic = datetime.datetime.now();
        time.sleep(60-datetime.datetime.now().second)

I have removed the "done" variable, since it conveys a meaning of finished. By reading your code, though, it seems you are actually pausing an ever ongoing process, actually.

Hope this helps!

Post a Comment for "Time As Stopping Condition Of Infinite Loop"