Python 控制环路的速度

Python 控制环路的速度,python,loops,time,while-loop,Python,Loops,Time,While Loop,我目前在大学里读物理,我学习python只是一个小爱好 为了同时练习这两种方法,我想我将编写一个小的“物理引擎”,它根据x、y和z坐标计算物体的运动。我只想返回文字的运动(至少现在!),但我希望位置更新是实时的 要做到这一点,我需要更新对象的位置,比如说每秒更新100次,然后将其打印回屏幕。所以每10毫秒程序就会打印当前位置 因此,如果计算的执行需要2毫秒,那么循环必须等待8毫秒才能打印并重新计算下一个位置 构建这样一个循环的最佳方式是什么?每秒100次是一个合理的频率,还是你会走慢一点,比如2

我目前在大学里读物理,我学习python只是一个小爱好

为了同时练习这两种方法,我想我将编写一个小的“物理引擎”,它根据x、y和z坐标计算物体的运动。我只想返回文字的运动(至少现在!),但我希望位置更新是实时的

要做到这一点,我需要更新对象的位置,比如说每秒更新100次,然后将其打印回屏幕。所以每10毫秒程序就会打印当前位置

因此,如果计算的执行需要2毫秒,那么循环必须等待8毫秒才能打印并重新计算下一个位置


构建这样一个循环的最佳方式是什么?每秒100次是一个合理的频率,还是你会走慢一点,比如25次/秒?

一个警告。您可能不希望在非实时系统上实现实时。
sleep
调用系列保证至少有一个给定的延迟,但很可能会延迟更多时间


因此,一旦您从睡眠中返回,查询当前时间,并将计算转入“未来”(计算时间)。

由于您无法提前知道每次迭代需要多长时间,因此需要某种事件驱动的循环。一种可能的解决方案是使用
twisted
模块,该模块基于


但是,正如已经指出的,不要期望真正的实时响应。

在python中等待的基本方法是
导入时间
并使用
时间。睡眠
。那么问题是,睡多久?这取决于您希望如何处理循环错过所需时间的情况。如果未达到目标间隔,以下实现将尝试赶上目标间隔

import time
import random

def doTimeConsumingStep(N):
    """
    This represents the computational part of your simulation.

    For the sake of illustration, I've set it up so that it takes a random
    amount of time which is occasionally longer than the interval you want.
    """
    r = random.random()
    computationTime = N * (r + 0.2)
    print("...computing for %f seconds..."%(computationTime,))
    time.sleep(computationTime)


def timerTest(N=1):
    repsCompleted = 0
    beginningOfTime = time.clock()

    start = time.clock()
    goAgainAt = start + N
    while 1:
        print("Loop #%d at time %f"%(repsCompleted, time.clock() - beginningOfTime))
        repsCompleted += 1
        doTimeConsumingStep(N)
        #If we missed our interval, iterate immediately and increment the target time
        if time.clock() > goAgainAt:
            print("Oops, missed an iteration")
            goAgainAt += N
            continue
        #Otherwise, wait for next interval
        timeToSleep = goAgainAt - time.clock()
        goAgainAt += N
        time.sleep(timeToSleep)

if __name__ == "__main__":
    timerTest()

请注意,在普通操作系统上,您将错过所需的计时,因此类似这样的事情是必要的。请注意,即使使用tulip和twisted等异步框架,您也无法保证在正常操作系统上进行计时。

发现了这一点;这将向您展示如何在Python中实现时间延迟。使用
time.sleep()
,从函数运行所需的时间中减去一个浮点,一个最小的print命令大约需要10毫秒。这是一个很好的解决方案,但我想知道对于初学者来说,这是否是正确的方法。
import time
import random

def doTimeConsumingStep(N):
    """
    This represents the computational part of your simulation.

    For the sake of illustration, I've set it up so that it takes a random
    amount of time which is occasionally longer than the interval you want.
    """
    r = random.random()
    computationTime = N * (r + 0.2)
    print("...computing for %f seconds..."%(computationTime,))
    time.sleep(computationTime)


def timerTest(N=1):
    repsCompleted = 0
    beginningOfTime = time.clock()

    start = time.clock()
    goAgainAt = start + N
    while 1:
        print("Loop #%d at time %f"%(repsCompleted, time.clock() - beginningOfTime))
        repsCompleted += 1
        doTimeConsumingStep(N)
        #If we missed our interval, iterate immediately and increment the target time
        if time.clock() > goAgainAt:
            print("Oops, missed an iteration")
            goAgainAt += N
            continue
        #Otherwise, wait for next interval
        timeToSleep = goAgainAt - time.clock()
        goAgainAt += N
        time.sleep(timeToSleep)

if __name__ == "__main__":
    timerTest()