pygame.midi 高 CPU 使用率
pygame.midi High CPU usage
在使用 pygame.midi 时,Python 消耗了我 CPU 的 20-25%。
我猜这是因为等待 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)
您可以使用 pygame.time.Clock.tick
来限制 CPU 的使用:
clock = pygame.time.Clock()
going = True
while going:
clock.tick(60)
# [...]
方法 tick()
of a pygame.time.Clock
对象,以这种方式延迟游戏,即循环的每次迭代都消耗相同的时间。
在使用 pygame.midi 时,Python 消耗了我 CPU 的 20-25%。
我猜这是因为等待 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)
您可以使用 pygame.time.Clock.tick
来限制 CPU 的使用:
clock = pygame.time.Clock()
going = True
while going:
clock.tick(60)
# [...]
方法 tick()
of a pygame.time.Clock
对象,以这种方式延迟游戏,即循环的每次迭代都消耗相同的时间。