0

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
1
  • test==5 will never be true as per the code given. Also time.sleep(timeout) will sleep for time.time() + 60*5 seconds. Commented Apr 10, 2016 at 17:21

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I think this should be a comment, not an answer.
The logic with description is actually a good one, because if there's nothing in file to read, then there will be nothing to execute with while statement.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.