简体   繁体   English

Python — time.sleep()由代码持续时间偏移

[英]Python — time.sleep() offset by code duration

I have a function that runs a tick() for all players and objects within my game server. 我有一个为游戏服务器中的所有玩家和对象运行tick()的函数。 I do this by looping through a set every .1 seconds. 我通过每隔.1秒循环遍历一组内容来完成此操作。 I need it to be a solid .1. 我需要它是坚实的1。 Lots of timing and math depends on this pause being as exact as possible to .1 seconds. 很多时间和数学方法都取决于此暂停尽可能精确到0.1秒。 To achieve this, I added this to the tick thread: 为此,我将其添加到了tick线程中:

start_time = time.time()

# loops and code and stuff for tick thread in here...

time_lapsed = time.time() - start_time # get the time it took to run the above code
if 0.1 - time_lapsed > 0:
    time.sleep(0.1 - time_lapsed)
else:
    print "Server is overloaded!"
    # server lag is greater that .1, so don't sleep, and just eat it on this run. 
    # the goal is to never see this.

My question is, is this the best way to do this? 我的问题是,这是最好的方法吗? If the duration of my loop is 0.01, then time_lapsed == 0.01 ... and then the sleep should only be for 0.09. 如果我的循环持续时间为0.01,则time_lapsed == 0.01 ...,则睡眠时间仅应为0.09。 I ask, because it doesn't seem to be working. 我问,因为它似乎没有用。 I started getting the overloaded server message the other day, and the server was most definitely not overloaded. 前几天,我开始收到服务器过载的消息,服务器肯定没有过载。 Any thoughts on a good way to "dynamically" control the sleep? 有什么想法可以“动态地”控制睡眠吗? Maybe there's a different way to run code every tenth of a second without sleeping? 也许有另一种方法可以每十分之一秒不睡觉地运行代码?

It would be better to base your "timing and math" on the amount of time actually passed since the last tick(). 最好根据自上次tick()以来实际经过的时间来进行“计时和数学计算”。 Depending on "very exact" timings will be fragile at the best of times. 根据“非常精确”的时机,在最佳情况下会很脆弱。

Update: what I mean is that your tick() method would take an argument, say "t", of the elapsed time since the last call. 更新:我的意思是,您的tick()方法将采用自上次调用以来经过的时间的参数,例如“ t”。 Then, to do movement you'd store each thing's position (say in pixels) and velocity (in "pixels/second") so the magnitude of its movement for that call to tick() becomes "velocity * t". 然后,要进行移动,您需要存储每个物体的位置(以像素为单位)和速度(以“像素/秒”为单位),以便对tick()的调用的移动量变为“速度* t”。

This has the additional benefit of decoupling your physics simulation from the frame-rate. 这具有将物理模拟与帧速率分离的额外好处。

I see pygame mentioned below: their "pygame.time.Clock.tick()" method is meant to be used this way, as it returns the number of seconds since the last time you called it. 我看到下面提到的pygame:他们的“ pygame.time.Clock.tick()”方法将以这种方式使用,因为它返回自上次调用以来的秒数。

Other Python threads may run in between leaving your thread less time. 其他Python线程可能会在这之间运行,从而使您的线程花费的时间更少。 Also time.time() is subject to system time adjustments; 而且time.time()受到系统时间调整的影响; it can be set back. 可以退后。

There is a similar function Clock.tick() in pygame . pygame中有一个类似的函数Clock.tick() Its purpose is to limit the maximum frame rate. 其目的是限制最大帧速率。

To avoid outside influence you could keep an independent frame/turn-based counter to measure the game time. 为了避免外界影响,您可以保留一个独立的基于帧/回合的计数器来衡量游戏时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM