I started from this: https://stackoverflow.com/questions/12435211/python-threading-timer-repeat-function-every-n-seconds#=
I want to send MIDI timecode, cross-platform. I checked the input logged from my code and the timing loses 100 milliseconds or so here and there, which is not nearly accurate enough - I need to send 96-120 times a second, evenly spaced messages. The input I get from Cubase is very, very accurate in this (max. 1 ms off after 10 seconds), but I have the feeling (I hope) I haven't yet found the best way to do it in Python. Also, I'd like the timing to be unaffected by the amount of processing taking place in between calls, which I guess will depend on how much musical activity is occurring at any given moment. But this is just timecode, no notes, and even so the timing is not accurate enough. I'm using Python 2.7 on Windows 7 (later on OSX 10.9 and Linux). Everywhere I look, I find results for benchmarking code or functions, but what I want is to execute some code 100+ times per second without drift. Any ideas? My code:
import time
import threading as th
ind = 0
class MyThread(th.Thread):
def __init__(self, event):
th.Thread.__init__(self)
self.stopped = event
self.ind = 0
def run(self):
while not self.stopped.wait(0.01):
self.ind += 1
if self.ind % 100 == 0:
print time.clock()
if self.ind == 1000:
self.stopped.set()
stopFlag = th.Event()
thread = MyThread(stopFlag)
thread.start()
Thanks a million! -Chuckk