Time.sleep value error after 1 hour

Hi everyone,

When I use time.sleep(5) in my while loop after an hour or so I get the following error:
Value error: sleep length must be non negative

Any help would be much appreciated !

Can you post the actual while loop you use? Copy and paste it into your
reply please, not a screen shot or photo.

And if possible, the full traceback you received, not just the last
line.

Also, what version of Python are you using?

I do not think that there is any way that

time.sleep(5)

can raise “Value error: sleep length must be non negative”, but I will
try to reproduce the error myself. I’ll write back after an hour.

Okay, I have run this for over an hour and a half:

while True:
    time.sleep(5)

and as I expected, there is no error. I think you need to show us the
actual code you are running.

Hi, thanks for your response. I’m using python 3.7
Here is my ‘while’ loop:


while True:
comp.getCurrentPrices()
comp.UpDownRatio()
comp.getBigShakeups()
comp.Editlist()
time.sleep(5)

Here is the exact error message:

Exception in thread Thread-599:
Traceback (most recent call last):
File “/home/exhile/anaconda3/envs/Bitvavo-Env/lib/python3.7/threading.py”, line 926, in _bootstrap_inner
self.run()
File “/home/exhile/anaconda3/envs/Bitvavo-Env/lib/python3.7/site-packages/python_bitvavo_api/bitvavo.py”, line 109, in run
self.waitForReset(self.timeToWait)
File “/home/exhile/anaconda3/envs/Bitvavo-Env/lib/python3.7/site-packages/python_bitvavo_api/bitvavo.py”, line 99, in waitForReset
time.sleep(waitTime)
ValueError: sleep length must be non-negative

Hi Gerald,

looks to me that the source of your error is another “sleep” - in the function “waitForReset” in the file bitvavo.py.

Cheers, Dominik

The failure has nothing to do with your call to time.sleep. It is coming
from this line:

File “/home/exhile/anaconda3/envs/Bitvavo-Env/lib/python3.7/site-packages/python_bitvavo_api/bitvavo.py”,
line 99, in waitForReset
time.sleep(waitTime)

To me, that looks like it may be a bug in bitvavo, but I haven’t looked
really deeply into it. I recommend you check the waitForReset
documentation and make sure you are using it correctly.

One hour = 60 minutes
60 minutes are (60x60) seconds.

In bash (of GNU Linux) this would look like :

while [<only-one-condition>]
do
sleep 3600
... <further conditions>
...
done

But sleep comes first before further conditions.
Only when 3600 seconds are over, then next conditions are done.
Greetz.

Many say it’s a common problem that time.sleep fails after a certain amount of time. They use a C wrapper to prevent the value from going below zero but nobody has given me a workable code yet.

Al I need is a simple wait command that doesn’t bug out after an hour or two. I never thought this would be so complicated.

Hi Gerald,

I never experienced that and I am using Multithreading/Multiprocessing heavily. The function time.sleep works well in your case too. You just should not call it with a negative number of seconds. So, this is the error of the programmer of bitvavo and not the error of the function time.sleep, if you ask me.

Cheers, Dominik

Gerald, you wrongly reported that the error was with

time.sleep(5)

inside a while loop that runs for more than an hour. That was wrong.
That will never fail, because 5 will never turn into a negative. You
misdiagnosed the error.

When you posted the actual line that fails, it wasn’t in your code, it
was in the library code that you call in another thread, where a
variable, not the constant 5, goes negative, and time.sleep is called
with that variable.

File "/home/exhile/anaconda3/envs/Bitvavo-Env/lib/python3.7/site-packages/python_bitvavo_api/bitvavo.py", 
line 99, in waitForReset
    time.sleep(waitTime)
ValueError: sleep length must be non-negative

It is impossible for us to tell from the limited information you have
given us whether that variable waitTime is being set by you or by the
bitvavo library. That is, we cannot tell whether your code sets that
variable to a negative number, so the bug is in your code and you can
fix it, or whether it is a bug in the library, and you will have to
report it to bitvavo or anaconda to fix.

It is just a coincidence that your code runs a while loop containing
time.sleep(5), the actual error is elsewhere.

I would start by reporting this to anaconda and see what they say.

You go on to say:

“Many say it’s a common problem that time.sleep fails after a certain
amount of time.”

Many people also say that the world is flat.

“They use a C wrapper to prevent the value from going below zero but
nobody has given me a workable code yet.”

That doesn’t even make sense. time.sleep is already calling C code, and
if all you want is a wrapper to prevent the value becoming negative, you
don’t need C for that:

def mysleep(arg):
    time.sleep(abs(arg))

will silently treat negative times as positive. (Note carefully that I
do not recommend this approach. Far better is to find out why your
sleep amount is becoming negative, because that is almost certainly a
bug in your code.)

“Al I need is a simple wait command that doesn’t bug out after an hour
or two. I never thought this would be so complicated.”

You are mistaken: you already have a simple wait command that doesn’t
bug out after an hour or two. The time.sleep function works correctly
for arguments above 1 second to at least tens of thousands of seconds,
that is, multiple days.

(Times below a second may not be perfectly accurate due to limitations
of the operating system, e.g. on Windows the lowest sleep resolution is
about 10 milliseconds.)

You can prove it yourself: run time.sleep(3*60*60) and it will sleep
for three hours.

The problem you are experiencing is not with a “simple wait command”, it
is a problem with a large, complicated library that uses multiple
threads. Either due to a bug in your code, or a bug in the library,
the wrong value is being passed to time.sleep, which correctly
raises an exception.

Welcome to the wonderful world of being a programmer.

1 Like

Unfortunately, that is (still) true… :joy: