Python:在特定时间内运行循环
我需要在一个精确的时间段内运行一个循环。例如,我想写的代码大概是这样的:
initialize and lauch timer
while timer<10000sec:
do things
你知道怎么做到这一点吗?
谢谢你 :)
2 个回答
1
import time
t_end = time.time() + 60 * <> #duration goes inside <>; eg 2 for 2 minutes
while time.time() < t_end:
#rest of the code
当然可以!请把你想要翻译的内容发给我,我会帮你用简单易懂的语言解释清楚。
6
stop = time.time()+10000
while time.time() < stop:
do things
这意味着每次循环的时间要足够短,这样你才能频繁地读取当前时间,以达到你想要的精确度。