How do I ensure that a Python while-loop takes a particular amount of time to run?

流过昼夜 提交于 2019-12-30 03:28:24

问题


I'm reading serial data with a while loop. However, I have no control over the sample rate.

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.

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

回答1:


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

Of course not 100% perfect (maybe off one millisecond or another from time to time), but I guess it's good enough.


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()



回答2:


At the beginning of the loop check if the appropriate amount of time has passed. If it has not, continue.

# 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:
        continue
    #read and print a line
    sample_value=ser.readline()
    sample_line=str(sample_time)+','+str(sample_value)
    outfile.write(sample_line)
    print 'time: ',sample_time,', value: ',sample_value
    next_min_time = sample_time + sample_period



回答3:


An rather elegant method is you're working on UNIX : use the signal library

The code :

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

And the timing :

 georgesl@cleese:~/Bureau$ time python timer.py
 5

 real   0m5.081s
 user   0m0.068s
 sys    0m0.004s

Two caveats though : it only works on *nix, and it is not multithread-safe.



来源:https://stackoverflow.com/questions/13197686/how-do-i-ensure-that-a-python-while-loop-takes-a-particular-amount-of-time-to-ru

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!