Python threading

From Noah.org
Revision as of 13:21, 7 June 2007 by Root (talk | contribs) (New page: Category:Engineering Category:Python == Basic threading example == <pre> import time import threading class roller (threading.Thread): def __init__(self, interval, function,...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Basic threading example

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): 
        """Stop the roller.""" 
        self.finished.set() 
    def run(self): 
        while not self.finished.isSet(): 
            self.finished.wait(self.interval) 
            self.function(*self.args, **self.kwargs)

r = roller (0.01, p.rotate_palette, (1,0,-1))
r.start()
time.sleep(60)
r.cancel()