time.sleep(x) 不能正常工作? [英] time.sleep(x) not working as it should?

查看:111
本文介绍了time.sleep(x) 不能正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我正在制作一个有趣的小程序,我想创建一个刷新按钮,允许用户控制收集和显示数据的频率.我决定使用 time.sleep(x) x 作为 raw_input 的内容.但它似乎并没有像它应该的那样工作.它会暂停完整的脚本,然后执行所有操作.

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.

例如:

import time

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

所以应该先打印第一部分,然后在 x 秒后打印第二部分.

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

但它会在 x 秒后立即打印所有内容.

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

当我使用 if 语句时,它似乎要等待额外的 x 秒以打印 if 语句中的任何内容.

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

当将任何高于 0 的旧数据按 x 量放入时,这真的会弄乱我的数据.例如,如果我输入 60,它将是一整分钟的旧数据(非实时).将其保留为 0 只会向控制台发送过多的垃圾邮件,尽管会使其无法阅读.

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?

推荐答案

这是因为输出缓冲.你应该关掉它.参考这个帖子:禁用输出缓冲

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

这篇关于time.sleep(x) 不能正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆