• <bdo id='1ykBh'></bdo><ul id='1ykBh'></ul>
    <tfoot id='1ykBh'></tfoot>
    <i id='1ykBh'><tr id='1ykBh'><dt id='1ykBh'><q id='1ykBh'><span id='1ykBh'><b id='1ykBh'><form id='1ykBh'><ins id='1ykBh'></ins><ul id='1ykBh'></ul><sub id='1ykBh'></sub></form><legend id='1ykBh'></legend><bdo id='1ykBh'><pre id='1ykBh'><center id='1ykBh'></center></pre></bdo></b><th id='1ykBh'></th></span></q></dt></tr></i><div id='1ykBh'><tfoot id='1ykBh'></tfoot><dl id='1ykBh'><fieldset id='1ykBh'></fieldset></dl></div>

  • <legend id='1ykBh'><style id='1ykBh'><dir id='1ykBh'><q id='1ykBh'></q></dir></style></legend>

        <small id='1ykBh'></small><noframes id='1ykBh'>

        如何确保 Python while 循环需要特定的时间才能运行?

        How do I ensure that a Python while-loop takes a particular amount of time to run?(如何确保 Python while 循环需要特定的时间才能运行?)
      1. <tfoot id='vFFQt'></tfoot>

                  <bdo id='vFFQt'></bdo><ul id='vFFQt'></ul>
                    <tbody id='vFFQt'></tbody>
                  <legend id='vFFQt'><style id='vFFQt'><dir id='vFFQt'><q id='vFFQt'></q></dir></style></legend>

                  <small id='vFFQt'></small><noframes id='vFFQt'>

                  <i id='vFFQt'><tr id='vFFQt'><dt id='vFFQt'><q id='vFFQt'><span id='vFFQt'><b id='vFFQt'><form id='vFFQt'><ins id='vFFQt'></ins><ul id='vFFQt'></ul><sub id='vFFQt'></sub></form><legend id='vFFQt'></legend><bdo id='vFFQt'><pre id='vFFQt'><center id='vFFQt'></center></pre></bdo></b><th id='vFFQt'></th></span></q></dt></tr></i><div id='vFFQt'><tfoot id='vFFQt'></tfoot><dl id='vFFQt'><fieldset id='vFFQt'></fieldset></dl></div>
                • 本文介绍了如何确保 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()
                  

                  这篇关于如何确保 Python while 循环需要特定的时间才能运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                  map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                  Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                  Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                  Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                  tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)
                        <tbody id='Jp9fB'></tbody>

                      <small id='Jp9fB'></small><noframes id='Jp9fB'>

                      <legend id='Jp9fB'><style id='Jp9fB'><dir id='Jp9fB'><q id='Jp9fB'></q></dir></style></legend>

                      <i id='Jp9fB'><tr id='Jp9fB'><dt id='Jp9fB'><q id='Jp9fB'><span id='Jp9fB'><b id='Jp9fB'><form id='Jp9fB'><ins id='Jp9fB'></ins><ul id='Jp9fB'></ul><sub id='Jp9fB'></sub></form><legend id='Jp9fB'></legend><bdo id='Jp9fB'><pre id='Jp9fB'><center id='Jp9fB'></center></pre></bdo></b><th id='Jp9fB'></th></span></q></dt></tr></i><div id='Jp9fB'><tfoot id='Jp9fB'></tfoot><dl id='Jp9fB'><fieldset id='Jp9fB'></fieldset></dl></div>
                        <bdo id='Jp9fB'></bdo><ul id='Jp9fB'></ul>

                            <tfoot id='Jp9fB'></tfoot>