简体   繁体   中英

How may i execute just 1 iteration when the condition is met?

I am using while true loop to produce an infinite loop. And, in the code, i have a section which is dependent on time. Here's the code:

while True:
    if currentminute == '53':
        # do this
    else:
        # do that

How may I make it just execute one loop of the 'do this' code if currentminute == '53' is true? And, execute 'do that' code which represents the bulk of all the other codes for the rest of the hour and come back to 'do this' code the next hour, etc.

from datetime import datetime
import time
while True:
    if datetime.datetime.now().minute == 53:
        # do this
        if datetime.datetime.now().minute == 53:  # if still 53 then sleep for rest of the seconds
            time.sleep(60-datetime.datetime.now().second)
    else:
        # do that

From your question, I assume that only the #do this part should be executed only once every 53 minute. Also you need to make sure that #do that part doesn't hold the execution for long so that it could overlap the 53rd minute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM