【发布时间】:2012-03-10 02:41:47
【问题描述】:
我正在尝试运行 python 应用程序并根据指定的时间间隔执行操作。下面的代码不断消耗 100% 的 CPU。
def action_print():
print "hello there"
interval = 5
next_run = 0
while True:
while next_run > time.time():
pass
next_run = time.time() + interval
action_print()
我想避免让进程进入睡眠状态,因为在不同的时间间隔会有更多的动作要执行。
请指教
【问题讨论】:
-
如果你不想以一种或另一种方式睡觉,你会烧毁 CPU。
-
您能否详细说明“将在不同的时间间隔执行更多操作”?
-
如何确保在进程休眠时所有内容都按时执行?我尝试使用睡眠,但是任何不到一秒的东西都会占用 CPU。
-
你想让这个进程占用 100% cpu 吗?
-
@larsmans - 我的意思是更多的动作即将到来,而不仅仅是打印。整个问题在于浮动间隔,即 3.5s、4.73s 等。
标签: python cpu consumption