'Python's time.sleep() hangs

I have a test script that sleeps for a random amount of seconds, between 1 and 180 seconds. The test program would hang for more than 3 minutes.

So I changed the code so that it would sleep for 60 seconds (1 minute) and ran the script. After 20 hours (!!!), the code is still hanging on time.sleep().

The code is

downtime = 60
time.sleep(downtime)

Why does Python hang forever on a time.sleep(60)?



Solution 1:[1]

You use 2 different variables downtime and downTime. Probably, downTime greater than 60

Solution 2:[2]

Even though time.sleep() is permitted to suspend your thread for an interval that is shorter, or longer, than what you ask, time.sleep(60) would not suspend your thread for 20 hours.

I can see several possible explanations:

  1. You are calling time.sleep() multiple times (e.g. in a loop).
  2. You are calling time.sleep() with an argument that is much higher than 60 (note the two different ways you're spelling downtime/downTime in your code).
  3. The problem has nothing to do with time.sleep().

Solution 3:[3]

It is dark under the lamp. The reason could be ridiculous.

Check if you ever clicked the shell screen.

Processes running on Powershell freezes randomly

Solution 4:[4]

In my case, I had commented some lines and introduced a bug due to a missing variable. For example:

# Some_code...
# my_var = True
time.sleep(1)
return my_var

This was not a hang due to sleep() but rather an error that was occurring in the worker nodes and the master node never got a response, waiting for a result that was never going to arrive. Checking the logs of the workers I could see the error and correct it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Maksym Polshcha
Solution 2 NPE
Solution 3 Константин Ван
Solution 4 Genarito