[英]How do I ensure that a Python while-loop takes a particular amount of time to run?
我正在使用 while 循环读取串行数据。 但是,我无法控制采样率。
代码本身似乎需要 0.2 秒才能运行,所以我知道我不能比这更快了。 但我希望能够精确控制我采样的速度有多慢。
我觉得我可以使用“睡眠”来做到这一点,但问题是,在不同的点上,循环本身可能需要更长的时间来读取(具体取决于通过串行数据传输的内容),因此代码将具有来弥补余额。
例如,假设我想每 1 秒采样一次,循环需要 0.2 秒到 0.3 秒的时间才能运行。 我的代码需要足够智能才能休眠 0.8 秒(如果循环需要 0.2 秒)或 0.7 秒(如果循环需要 0.3 秒)。
import serial
import csv
import time
#open serial stream
while True:
#read and print a line
sample_value=ser.readline()
sample_time=time.time()-zero
sample_line=str(sample_time)+','+str(sample_value)
outfile.write(sample_line)
print 'time: ',sample_time,', value: ',sample_value
只需测量运行代码所需的每次循环迭代时间,并相应地sleep
:
import time
while True:
now = time.time() # get the time
do_something() # do your stuff
elapsed = time.time() - now # how long was it running?
time.sleep(1.-elapsed) # sleep accordingly so the full iteration takes 1 second
当然不是 100% 完美(有时可能会降低一毫秒或其他时间),但我想这已经足够了。
另一个不错的方法是使用twisted的LoopingCall
:
from twisted.internet import task
from twisted.internet import reactor
def do_something():
pass # do your work here
task.LoopingCall(do_something).start(1.0)
reactor.run()
一个相当优雅的方法是你在 UNIX 上工作:使用信号库
代码 :
import signal
def _handle_timeout():
print "timeout hit" # Do nothing here
def second(count):
signal.signal(signal.SIGALRM, _handle_timeout)
signal.alarm(1)
try:
count += 1 # put your function here
signal.pause()
finally:
signal.alarm(0)
return count
if __name__ == '__main__':
count = 0
count = second(count)
count = second(count)
count = second(count)
count = second(count)
count = second(count)
print count
和时间:
georgesl@cleese:~/Bureau$ time python timer.py
5
real 0m5.081s
user 0m0.068s
sys 0m0.004s
但是有两个警告:它仅适用于 *nix,并且不是多线程安全的。
在循环开始时检查是否经过了适当的时间量。 如果没有,请sleep
。
# Set up initial conditions for sample_time outside the loop
sample_period = ???
next_min_time = 0
while True:
sample_time = time.time() - zero
if sample_time < next_min_time:
time.sleep(next_min_time - sample_time)
continue
# read and print a line
sample_value = ser.readline()
sample_line = str(sample_time)+','+str(sample_value)
outfile.write(sample_line)
print 'time: {}, value: {}'.format(sample_time, sample_value)
next_min_time = sample_time + sample_period
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.