【问题标题】:pygame.midi High CPU usagepygame.midi CPU使用率高
【发布时间】:2021-12-09 09:19:07
【问题描述】:

在使用 pygame.midi 时,Python 占用了我 20-25% 的 CPU。

我猜这是因为等待 MIDI 输入的“While”循环...

有什么想法吗?如果您有任何建议,我将不胜感激...

这是循环:

    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)

【问题讨论】:

    标签: python pygame midi


    【解决方案1】:

    您可以使用pygame.time.Clock.tick 限制 CPU 使用率:

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

    pygame.time.Clock 对象的方法tick() 以这种方式延迟游戏,即循环的每次迭代消耗相同的时间段。

    【讨论】:

    • 你是如何扛起天才的重担的?
    猜你喜欢
    • 2014-07-16
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多