如何确保 Python while 循环需要特定的时间才能运行?
问题描述
我正在使用 while 循环读取串行数据.但是,我无法控制采样率.
I'm reading serial data with a while loop. However, I have no control over the sample rate.
代码本身似乎需要 0.2 秒才能运行,所以我知道我不会比这更快.但我希望能够精确控制采样的速度.
The code itself seems to take 0.2s to run, so I know I won't be able to go any faster than that. But I would like to be able to control precisely how much slower I sample.
我觉得我可以使用睡眠"来做到这一点,但问题是循环本身在不同点可能需要更长的时间来读取(具体取决于通过串行数据传输的内容),所以代码必须弥补余额.
I feel like I could do it using 'sleep', but the problem is that there is potential that at different points the loop itself will take longer to read(depending on precisely what is being transmitted over serial data), so the code would have to make up the balance.
例如,假设我想每 1 秒采样一次,并且循环需要 0.2 秒到 0.3 秒的时间来运行.我的代码需要足够聪明才能休眠 0.8 秒(如果循环需要 0.2 秒)或 0.7 秒(如果循环需要 0.3 秒).
For example, let's say I want to sample every 1s, and the loop takes anywhere from 0.2s to 0.3s to run. My code needs to be smart enough to sleep for 0.8s (if the loop takes 0.2s) or 0.7s (if the loop takes 0.3s).
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
:
Just measure the time running your code takes every iteration of the loop, and sleep
accordingly:
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% 完美(有时可能会缩短一毫秒或更多),但我想这已经足够了.
Of course not 100% perfect (maybe off one millisecond or another from time to time), but I guess it's good enough.
另一个不错的方法是使用 twisted 的 LoopingCall
:
Another nice approach is using twisted's 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()
相关文章