multithreading - Thread priority in Python -
for music sampler, have 2 main threads (using threading
) :
thread #1 reads sound files from disk, in real-time, when needed (example: when press c#3 on midi keyboard, need play c#3.wav possible!) or ram if sound has been loaded in ram,
thread #2 preloads files ram, 1 after another.
thread #2 should done in background, during free time, should not prevent thread #1 job quickly.
in short, thread #1 should have higher priority thread #2.
how threading
or other python thread managament module? or possible pthread_setschedparam
? how?
i'm not big expert i'll handle problem this:
#!/usr/bin/python2.7 # coding: utf-8 import threading, time class foo: def __init__(self): self.allow_thread1=true self.allow_thread2=true self.important_task=false threading.thread(target=self.thread1).start() threading.thread(target=self.thread2).start() def thread1(self): loops=0 while self.allow_thread1: in range(10): print ' thread1' time.sleep(0.5) self.important_task=true in range(10): print 'thread1 important task' time.sleep(0.5) self.important_task=false loops+=1 if loops >= 2: self.exit() time.sleep(0.5) def thread2(self): while self.allow_thread2: if not self.important_task: print ' thread2' time.sleep(0.5) def exit(self): self.allow_thread2=false self.allow_thread1=false print 'bye bye' exit() if __name__ == '__main__': foo()
in few words, i'll handle thread2
thread1
. if thread1
busy, pause thread2
.
note added loops
kill thread in example, in real case exit function called when closing program. ( if threads running in background)
Comments
Post a Comment