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

不想你离开。 提交于 2019-12-18 16:47:13

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/21886233/time-sleepx-not-working-as-it-should

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!