Pyglet 使用过多的 CPU [英] Pyglet uses too much cpu

查看:58
本文介绍了Pyglet 使用过多的 CPU的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始从 pygame 进入 pyglet 和 rabbyt,但我遇到了一些障碍.

I recently startet getting into pyglet and rabbyt from pygame, but I have hit something of a brick wall.

我创建了一个基本示例,其中一个 Sprite(在 pyglet.sprite.Sprite 中找到的类型)以每秒 60 帧的速度显示.问题是这个简单的程序不知何故占用了 50% 的 CPU 时间.我用在rabbyt库中找到的精灵类型重复了实验,结果相同.

I created a basic example where one Sprite (of the type found in pyglet.sprite.Sprite) is displayed at 60 frames per second. The problem is that this simple program is using up 50% of the CPU time somehow. I repeated the experiment with the sprite type found in the rabbyt library with the same result.

我决定以每秒 60 帧的速度渲染 1000 个,然后是 10000 个精灵,令我惊讶的是 CPU 使用率保持在 50%.唯一的问题是移动或动画精灵会导致轻微的卡顿.

I decided to render 1000, then 10 000 sprites at 60 frames per second, and to my surprise the CPU usage stays at 50%. The only thing is that moving or animating a sprite results in slight stuttering.

最后,我尝试以每秒 360 帧的速度运行.结果相同,使用率为 50%.

Lastly, I tried running at 360 frames per second. Same result, 50% usage.

这是示例代码:

import pyglet
import rabbyt


def on_draw(dt):
    window.clear()
    spr.render()

global window
window = pyglet.window.Window(800, 600)
spr = rabbyt.Sprite('ship.png')
spr.x = 100
spr.y = 100
pyglet.clock.schedule_interval(on_draw, 1.0/60.0)


if __name__ == '__main__':
    pyglet.app.run()

我使用的是 Core 2 Duo 和 ATI HD 3500 卡.

I am using a Core 2 Duo with an ATI HD 3500 card.

感谢任何建议/想法.

推荐答案

请注意,默认的 pyglet 事件处理程序每​​次清除事件队列时都会触发 'on_draw' 事件.

Be aware that the default pyglet event handler will fire an 'on_draw' event every time it clears the event queue.

http://www.pyglet.org/doc/programming_guide/the_application_event_loop.html

pyglet 应用程序事件循环在窗口事件(例如鼠标和键盘输入)发生时分派,并在每次循环迭代后将 on_draw 事件分派到每个窗口.

The pyglet application event loop dispatches window events (such as for mouse and keyboard input) as they occur and dispatches the on_draw event to each window after every iteration through the loop.

这意味着任何事件都可以触发重绘.

因此,如果您正在移动鼠标或执行任何触发事件的操作,当它开始触发渲染调用时,您的速度会大大减慢.

So if you're moving the mouse about or doing anything that fires events, you will get massive slow down as it begins to trigger render calls.

这也引起了问题,因为我正在执行自己的渲染调用,所以我会让两个缓冲区发生冲突,从而在屏幕上产生幽灵"效果.我花了一段时间才意识到这是原因.

This also caused problems because I was doing my own render calls, so I would get the two buffers fighting which created a 'ghost' effect on the screen. Took me a while to realise this was the cause.

我猴子修补了事件循环以不这样做.https://github.com/adamlwgriffiths/PyGLy/blob/master/pygly/monkey_patch.py​​

I monkey patched the event loop to not do this. https://github.com/adamlwgriffiths/PyGLy/blob/master/pygly/monkey_patch.py

请注意,此修补后的事件循环将不再自行呈现,您必须手动翻转缓冲区或触发on_draw"事件.

Be aware that this patched event loop will no longer render on it's own, you must manually flip the buffers or trigger an 'on_draw' event.

可能的情况是,虽然您以 60fps 的速度进入,但内部渲染循环正在以最大可能的速率滴答作响.

It might be the case that, although you've hooked in at 60fps, but the internal render loop is ticking at the maximum possible rate.

我不喜欢剥夺控制权的代码,因此我的补丁让我决定何时发生渲染事件.

I dislike code that takes away control, hence my patch lets me decide when render events occur.

这篇关于Pyglet 使用过多的 CPU的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆