I'm writing a Python script that takes action when I press a key on my MIDI keyboard:
# https://github.com/superquadratic/rtmidi-python/
import rtmidi_python as rtmidi
def callback(message, time_stamp):
print message, time_stamp
midi_in = rtmidi.MidiIn()
midi_in.callback = callback
midi_in.open_port(0)
# do something else here (but don't quit)
Now, how to keep the script alive, so that it responsively handles callbacks, while at the same time not draining unnecessary CPU by spinning its wheels?
From my early Visual Studio days I remember a 'while( true ) { doevents(); }' type construct -- the operating system cycles through active processes allocating a certain time slice to each, and this command simply returns control to the scheduler.
But I am on OSX, and I would like to make a platform independent Python script.
Can I?