简体   繁体   中英

Python While Loop Condition Using Time

I want to make a while loop which uses time.sleep() every 5 minutes then continues the process. But the problem is that my code is not working properly. I'm using this code below. What can be the problem in my code and what should I do to make it work?

with open('url.txt', 'r', encoding='UTF-8', errors='ignore') as des:
    description = des.readline()
    timeout = time.time() + 60*5
    while description:
        test = 0
        description = des.readline()
        browser.get(description)       
        time.sleep(6)

        if test == 5 or time.time() > timeout:
            time.sleep(timeout)
            continue

Without seeing all your code or having more description, it is difficult to determine what the problem is. If you want the loop to execute once overy five minutes, you would use time.sleep(5*60) because time.sleep() works in seconds, not minutes. Also, if test == 5 will never be true because you set test = 0 in the loop, then never change it. If description is False , than the while loop will never execute. Make sure that description is true and that you change the test variable or remove it from the if statement. It also looks like to repeat the line description = des.readline() , which should not be changing at all.

I think it's because of your time.sleep(timeout) line.

It basically means - pause for seconds from epoch + 5minutes, therefore it'll run only one time and then it runs almost something like time.sleep(infinity) - at least for your machine.

It sleeps well.. for a long time.

Also, the test variable will never be ==5 , because you are not incrementing it. Move the variable outside while and after browser.get(description) put test += 1 . And in the second line is a typo, it should be des.readlines() if I'm right.

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