简体   繁体   English

pygame.midi CPU使用率高

[英]pygame.midi High CPU usage

While using pygame.midi, Python consumes 20-25% of my CPU.在使用 pygame.midi 时,Python 占用了我 20-25% 的 CPU。

I guess it's because of the "While" loop which waits for MIDI input...我想这是因为等待 MIDI 输入的“While”循环......

Any ideas?有任何想法吗? I'd appreciate any advice you might have...我很感激你的任何建议......

Here is the loop:这是循环:

    going = True
    while going:
        events = event_get()
        for e in events:
            if e.type in [pg.QUIT]:
                going = False
            if e.type in [pg.KEYDOWN]:
                going = False
            if e.type in [pygame.midi.MIDIIN]:
                if e.data2 == 127:
                    shortcuts(e.data1)

        if i.poll():
            midi_events = i.read(10)
            # convert them into pygame events.
            midi_evs = pygame.midi.midis2events(midi_events, i.device_id)

            for m_e in midi_evs:
                event_post(m_e)

You can limit the CPU usage with pygame.time.Clock.tick :您可以使用pygame.time.Clock.tick限制 CPU 使用率:

clock = pygame.time.Clock()
going = True
while going:
    clock.tick(60)

    # [...]

The method tick() of a pygame.time.Clock object, delays the game in that way, that every iteration of the loop consumes the same period of time. pygame.time.Clock对象的方法tick()以这种方式延迟游戏,即循环的每次迭代消耗相同的时间段。

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

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