Josh Miller
Josh Miller

Reputation: 19

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

Upvotes: 0

Views: 556

Answers (2)

Peter Badida
Peter Badida

Reputation: 12169

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.

Upvotes: 1

cat40
cat40

Reputation: 318

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.

Upvotes: 1

Related Questions