简体   繁体   English

难以理解如何传递数据

[英]Difficulty understanding how data is passed

So I'm trying to have a strobe like effect on a game I'm building and the way I currently have it it's destroying my frame rate because the sleep function is also applying to the draw function. 因此,我试图在正在构建的游戏上产生类似频闪的效果,而目前的使用方式却破坏了帧速率,因为睡眠功能也适用于平局功能。 Can someone explain why this happens? 有人可以解释为什么会这样吗? And the logic that I'm failing to understand. 还有我无法理解的逻辑。 Why can't I just have the return happen every .5 seconds without it affecting the .1 sleep I have in my hue function? 为什么我不能每隔0.5秒返回一次,而又不影响我的色相功能中的.1睡眠?

Here's a crude demonstration of what the code kind of does. 这是代码的功能的粗略演示。

from random import randint
import time
def rand_intr():
    r = randint(1,256)
    time.sleep(.5)
    return r

def rand_intg():
    g = randint(1,256)
    time.sleep(.5)
    return g

def rand_intb():
    b = randint(1,256)
    time.sleep(.5)
    return b

def hue():
    r = rand_intr()
    g = rand_intg()
    b = rand_intb()
    print(r, g, b)
    print('test')
    time.sleep(.01)

while True:
    hue()

The sleep function blocks the main thread. sleep功能会阻塞主线程。 This means rand_intg does not run until rand_intr "wakes up" from its sleep. 这意味着rand_intgrand_intr从其睡眠中“唤醒”之前不会运行。 Similarly, rand_intb has to wait for rand_intg , and hue has to wait for all the previous 3 functions. 同样, rand_intb必须等待rand_intg ,而hue必须等待所有前面的3个函数。 This means the total time hue has to wait before it can do any work is at least the amount of time needed to complete rand_intr , rand_intg , and rand_intb . 这意味着hue必须等待的总时间至少是完成rand_intrrand_intgrand_intb所需的时间。

We can understand what is happening if we modify your example slightly and look at the output. 如果我们稍微修改您的示例并查看输出,我们可以理解发生了什么。

from random import randint
import time

def log_entry_exit(f):
    def wrapped():
        print("Entered {}".format(f.__name__))
        result = f()
        print("Leaving {}".format(f.__name__))
        return result
    return wrapped

@log_entry_exit
def rand_intr():
    r = randint(1,256)
    time.sleep(.5)
    return r

@log_entry_exit
def rand_intg():
    g = randint(1,256)
    time.sleep(.5)
    return g

@log_entry_exit
def rand_intb():
    b = randint(1,256)
    time.sleep(.5)
    return b

def hue():
    r = rand_intr()
    g = rand_intg()
    b = rand_intb()
    print(r, g, b)
    print('test')
    time.sleep(.01)

while True:
    hue()

Here I just modified your functions to print a message when we enter and exit each function. 在这里,我只是修改了您的函数以在我们进入和退出每个函数时打印一条消息。

The output is 输出是

Entered rand_intr
Leaving rand_intr
Entered rand_intg
Leaving rand_intg
Entered rand_intb
Leaving rand_intb
172 206 115
test
Entered rand_intr
Leaving rand_intr
Entered rand_intg
Leaving rand_intg
Entered rand_intb
Leaving rand_intb
240 33 135
test
...

Here, the effect of each sleep on hue can be seen clearly. 在这里,可以清楚地看到每次sleephue的影响。 You don't get to print the rgb values or "test" until the previous functions have completed. 在完成之前的功能之前,您无法打印rgb值或“测试”。

What you can do is to call your hue function periodically using a timer callback, and then modify the rgb values according to some pattern. 您可以做的是使用计时器回调定期调用hue函数,然后根据某种模式修改rgb值。 See this stackoverflow question on executing periodic actions for an example on how to periodically execute a function using a basic time-based mechanism. 有关如何使用基本的基于时间的机制定期执行功能的示例,请参见有关执行定期操作的 stackoverflow问题。

Edit 编辑

Based on your comment to @jasonharper 根据您对@jasonharper的评论

If you call hue every 60 seconds, it does not make sense if your calls to the functions that generate the random rgb values occur at a faster rate because any changes in the intervening time will not be seen in hue . 如果您每60秒调用一次hue ,则对生成随机rgb值的函数的调用以更快的速度发生是没有意义的,因为在hue中将看不到中间时间的任何变化。

What you can do is call hue every 60 seconds, then generate your rgb values to have whatever pattern in there. 您可以做的是每60秒调用一次hue ,然后生成rgb值以在其中具有任何模式。

Modifying the answer by @kev in the post I linked to above, 我在上面链接的帖子中通过@kev修改了答案,

import time, threading
def update():
    # Do whatever you want here. 
    # This function will be called again in 60 seconds.
    # ...

    hue()

    # Whatever other things you want to do
    # ...

    threading.Timer(60.0, update).start()

def hue():
    r = rand_intr()
    g = rand_intg()
    b = rand_intb()
    print(r, g, b)
    # Don't call sleep.

if __name__ == "__main__":
    update()

Now you should only call update once , possibly in some startup part of your code and remove all the calls to sleep in your functions. 现在,您应该只调用一次 update (可能在代码的某些启动部分),并删除所有调用以使函数进入sleep

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

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