Difference between revisions of "Python threading"
From Noah.org
Jump to navigationJump to searchLine 8: | Line 8: | ||
<pre> | <pre> | ||
#!/usr/bin/env python | #!/usr/bin/env python | ||
+ | |||
+ | """This demonstrates running a function in a thread loop. This will call a | ||
+ | given function over and over in an infinite loop. It will optionally sleep | ||
+ | between each call. It will also check a semaphore to see if it should stop. | ||
+ | """ | ||
import time | import time |
Revision as of 15:44, 26 June 2007
Basic threading example
This is a simple example of threading in Python. This runs my_function() in a thread loop.
#!/usr/bin/env python """This demonstrates running a function in a thread loop. This will call a given function over and over in an infinite loop. It will optionally sleep between each call. It will also check a semaphore to see if it should stop. """ import time import threading class thread_looper (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 stop (self): self.finished.set() self.join() 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." t = thread_looper (0.1, my_function, (1,0,-1)) t.start() # The thread, t, runs while we are asleep. time.sleep(2) t.stop() print "Done!"
Example output:
Calling my_function() in a thread every 1/10th of second for two seconds. meaningless arguments as an example: 1 0 -1 Thu Jun 7 13:34:33 2007 meaningless arguments as an example: 1 0 -1 Thu Jun 7 13:34:33 2007 meaningless arguments as an example: 1 0 -1 Thu Jun 7 13:34:33 2007 meaningless arguments as an example: 1 0 -1 Thu Jun 7 13:34:33 2007 meaningless arguments as an example: 1 0 -1 Thu Jun 7 13:34:33 2007 meaningless arguments as an example: 1 0 -1 Thu Jun 7 13:34:33 2007 meaningless arguments as an example: 1 0 -1 Thu Jun 7 13:34:33 2007 meaningless arguments as an example: 1 0 -1 Thu Jun 7 13:34:33 2007 meaningless arguments as an example: 1 0 -1 Thu Jun 7 13:34:34 2007 meaningless arguments as an example: 1 0 -1 Thu Jun 7 13:34:34 2007 meaningless arguments as an example: 1 0 -1 Thu Jun 7 13:34:34 2007 meaningless arguments as an example: 1 0 -1 Thu Jun 7 13:34:34 2007 meaningless arguments as an example: 1 0 -1 Thu Jun 7 13:34:34 2007 meaningless arguments as an example: 1 0 -1 Thu Jun 7 13:34:34 2007 meaningless arguments as an example: 1 0 -1 Thu Jun 7 13:34:34 2007 meaningless arguments as an example: 1 0 -1 Thu Jun 7 13:34:34 2007 meaningless arguments as an example: 1 0 -1 Thu Jun 7 13:34:34 2007 meaningless arguments as an example: 1 0 -1 Thu Jun 7 13:34:34 2007 meaningless arguments as an example: 1 0 -1 Thu Jun 7 13:34:35 2007 Done! meaningless arguments as an example: 1 0 -1 Thu Jun 7 13:34:35 2007