蟒蛇->一段时间循环已经运行

发布于 2024-10-12 15:44:14 字数 127 浏览 7 评论 0原文

我有一个循环,一次运行长达几个小时。我怎样才能让它以设定的时间间隔告诉我已经过去了多长时间?

只是一个通用的...问题

编辑:这是一个运行排列的 while 循环,所以我可以让它打印每 10 秒运行的时间吗?

i have a loop that runs for up to a few hours at a time. how could I have it tell me how long it has been at a set interval?

just a generic...question

EDIT: it's a while loop that runs permutations, so can i have it print the time running every 10 seconds?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

埖埖迣鎅 2024-10-19 15:44:14

您可以使用 Timer 对象,而不是检查每个循环的时间

import time
from threading import Timer

def timeout_handler(timeout=10):
    print time.time()
    timer = Timer(timeout, timeout_handler)
    timer.start()

timeout_handler()
while True:
    print "loop"
    time.sleep(1)

Instead of checking the time on every loop, you can use a Timer object

import time
from threading import Timer

def timeout_handler(timeout=10):
    print time.time()
    timer = Timer(timeout, timeout_handler)
    timer.start()

timeout_handler()
while True:
    print "loop"
    time.sleep(1)
快乐很简单 2024-10-19 15:44:14

如前所述,这是一个有点令人讨厌的黑客行为,因为它涉及检查每次迭代的时间。为了使其工作,您需要让任务在一小部分超时时间内运行 - 如果您的循环仅每分钟迭代一次,则它不会每十秒打印一次。如果你想被中断,你可以考虑多线程,或者最好是如果你使用的是 linux/mac/unix,信号。你的平台是什么?

import time

timeout = 10
first_time = time.time()
last_time = first_time
while(True):
    pass #do something here
    new_time = time.time()
    if  new_time - last_time > timeout:
        last_time = new_time
        print "Its been %f seconds" % (new_time - first_time)

输出:

Its been 10.016000 seconds
Its been 20.031000 seconds
Its been 30.047000 seconds

As noted, this is a little bit of a nasty hack, as it involves checking the time every iteration. In order for it to work, you need to have tasks that run for a small percentage of the timeout - if your loop only iterates every minute, it won't print out every ten seconds. If you want to be interrupted, you might consider multithreading, or preferably if you are on linux/mac/unix, signals. What is your platform?

import time

timeout = 10
first_time = time.time()
last_time = first_time
while(True):
    pass #do something here
    new_time = time.time()
    if  new_time - last_time > timeout:
        last_time = new_time
        print "Its been %f seconds" % (new_time - first_time)

Output:

Its been 10.016000 seconds
Its been 20.031000 seconds
Its been 30.047000 seconds
不如归去 2024-10-19 15:44:14

有一种非常hacky的方法可以使用time.asctime()来做到这一点。您可以在进入 while 循环之前以及循环本身的某个位置存储 asctime。计算存储的时间和当前时间之间的时间差,如果差值是 10 秒,则将存储的时间更新为当前时间并打印它正在运行。

然而,这是一种非常老套的方法,因为它需要一些扭曲且无聊的数学。

如果您的目标是检查特定算法的运行时间,那么您最好使用 timeit 模块

希望这有帮助

There's a very hacky way to do this by using time.asctime(). You store the asctime before entering the while loop and somewhere in the loop itself. Calculate the time difference between the stored time and the current time and if that difference is 10 seconds, then update the stored time to the current time and print that it's been running.

However, that's a very hacky way to do it as it requires some twisted and boring math.

If your aim is to check the runtime of a specific algorithm, then you're better off using the timeit module

Hope this Helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文