简体   繁体   中英

pygame.midi High CPU usage

While using pygame.midi, Python consumes 20-25% of my CPU.

I guess it's because of the "While" loop which waits for MIDI input...

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 :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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