For a music sampler, I have two main threads (using threading
) :
Thread #1 reads sound from files from disk, in real-time, when needed (example: when we press a C#3 on the MIDI keyboard, we need to play C#3.wav as soon as possible!) or from RAM if this sound has been already loaded in RAM,
Thread #2 preloads all the files into RAM, one after another.
Thread #2 should be done in background, only during free time, but should not prevent Thread #1 to do its job quickly.
In short, Thread #1 should have much higher priority than Thread #2.
How to do that with threading
or any other Python thread managament module? Or is it possible with pthread_setschedparam
? How?
I'm not a big expert but I'll handle that problem like this:
In a few words, I'll handle
thread2
withthread1
. Ifthread1
is busy, then we pausethread2
.Note that I added
loops
just to kill the thread in the example, but in a real case the exit function would be called when closing the program. ( if your threads are always running in the background)