Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

time.sleep(x) not working as it should? [duplicate]

Okay, so I'm making a small programme for fun and I'd like to create a refresh button that allows the user to control how regularly the data is collected and shown. I decided to use the time.sleep(x) x being what the raw_input was. But it doesn't seem to be working as it should. It pauses the full script then does everything.

eg:

import time

print "This now"
time.sleep(x)
print "and this after x amount of  seconds"

So should print the first part then the second after x amount of seconds.

But instead it prints all of it at once after x amount of seconds.

When I use a if statement after it seems to wait an extra x amount seconds for printing whatever's in the if statement.

This really messes up my data when put in anything higher than 0 its old data by x amount. For example, if I put in 60 it would be a whole minute old data (not live). Leaving it at 0 just spams the console with too much though making it impossible to read.

Any idea why and how to fix my problem?

like image 360
Help a Frog out Avatar asked Feb 19 '14 16:02

Help a Frog out


People also ask

Does time sleep block all threads?

Python time sleep function is used to add delay in the execution of a program. We can use python sleep function to halt the execution of the program for given time in seconds. Notice that python time sleep function actually stops the execution of current thread only, not the whole program.

How do you make Python wait 5 seconds?

If you've got a Python program and you want to make it wait, you can use a simple function like this one: time. sleep(x) where x is the number of seconds that you want your program to wait.

Does time sleep stop all code?

Just note that delays created with time. sleep() do not stop the execution of the whole program – they only delay the current thread.

How to set time sleep in Python?

Adding a Python sleep() Call With time.sleep() Python has built-in support for putting your program to sleep. The time module has a function sleep() that you can use to suspend execution of the calling thread for however many seconds you specify.


1 Answers

This is because of output buffering. You should turn it off. Refer to this post: Disable output buffering

like image 149
Jayanth Koushik Avatar answered Oct 06 '22 00:10

Jayanth Koushik