蟒蛇->一段时间循环已经运行
我有一个循环,一次运行长达几个小时。我怎样才能让它以设定的时间间隔告诉我已经过去了多长时间?
只是一个通用的...问题
编辑:这是一个运行排列的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 Timer 对象,而不是检查每个循环的时间
Instead of checking the time on every loop, you can use a Timer object
如前所述,这是一个有点令人讨厌的黑客行为,因为它涉及检查每次迭代的时间。为了使其工作,您需要让任务在一小部分超时时间内运行 - 如果您的循环仅每分钟迭代一次,则它不会每十秒打印一次。如果你想被中断,你可以考虑多线程,或者最好是如果你使用的是 linux/mac/unix,信号。你的平台是什么?
输出:
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?
Output:
有一种非常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
moduleHope this Helps