time.sleep() 是否停止所有执行? [英] Does time.sleep() stop all executions?

查看:40
本文介绍了time.sleep() 是否停止所有执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我复杂的 python 程序中,当它运行时,我有一段代码每 3 秒执行一次,将程序的进度打印为已完成的执行百分比,如下所示:

In my complex python program, when it's running, I have a piece of code that executes every 3 seconds that prints the program's progress as the percentage of the execution that's finished like so:

while len(dequeueingFinishedList)!=10:
    print(str(len(masterListCSV_RowsListFinished)/float(len(masterListCSV_RowsList))*100) + "% done.")
    time.sleep(3)

time.sleep() 函数会减慢我的程序吗?我读到 sleep 函数会暂停执行.如果它减慢了我的程序速度,是否有更正确的方法每 3 秒向我打印一次进度?

Is the time.sleep() function going to slow down my program? I read the that sleep function suspends execution. If it is slowing down my program, is there a more correct way of printing the progress to me every 3 seconds?

推荐答案

from time import time

prev = time()
while True:
    now = time()
    if now - prev > 3:
        print 'report'
        prev = now
    else:
        pass
        # runs

这篇关于time.sleep() 是否停止所有执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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