Python threading
From Noah.org
Basic threading example
This runs my_function in a thread loop. Note that you might see "Done!" before the last line of thread output. Welcome to threadland!
#!/usr/bin/env python import time import threading class roller (threading.Thread): def __init__(self, interval, function, args=[], kwargs={}): threading.Thread.__init__(self) self.interval = interval self.function = function self.args = args self.kwargs = kwargs self.finished = threading.Event() def cancel(self): self.finished.set() def run(self): while not self.finished.isSet(): self.finished.wait(self.interval) self.function(*self.args, **self.kwargs) def my_function (a, b, c): print "meaningless arguments as an example:", a, b, c print time.asctime() print "Calling my_function() in a thread every 1/10th of second for two seconds." r = roller (0.1, my_function, (1,0,-1)) r.start() time.sleep(2) r.cancel() print "Done!"