python - >一个while循环一直在运行

tek*_*agi 6 python time loops permutation while-loop

我有一个循环,一次运行几个小时.我怎么能告诉我它在设定的时间间隔内有多长时间?

只是一个通用的问题

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

kef*_*hou 8

您可以使用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)
Run Code Online (Sandbox Code Playgroud)