Difference between revisions of "Python threading"
From Noah.org
Jump to navigationJump to searchLine 5: | Line 5: | ||
This is a simple example of threading in Python. | This is a simple example of threading in Python. | ||
This runs my_function() in a thread loop. | This runs my_function() in a thread loop. | ||
− | |||
− | |||
<pre> | <pre> | ||
Line 15: | Line 13: | ||
class thread_looper (threading.Thread): | class thread_looper (threading.Thread): | ||
− | def __init__(self, interval, function, args=[], kwargs={}): | + | def __init__ (self, interval, function, args=[], kwargs={}): |
threading.Thread.__init__(self) | threading.Thread.__init__(self) | ||
self.interval = interval | self.interval = interval | ||
Line 22: | Line 20: | ||
self.kwargs = kwargs | self.kwargs = kwargs | ||
self.finished = threading.Event() | self.finished = threading.Event() | ||
− | def | + | def stop (self): |
self.finished.set() | self.finished.set() | ||
− | def run(self): | + | self.join() |
+ | def run (self): | ||
while not self.finished.isSet(): | while not self.finished.isSet(): | ||
self.finished.wait(self.interval) | self.finished.wait(self.interval) | ||
Line 38: | Line 37: | ||
# The thread, t, runs while we are asleep. | # The thread, t, runs while we are asleep. | ||
time.sleep(2) | time.sleep(2) | ||
− | t. | + | t.stop() |
print "Done!" | print "Done!" | ||
</pre> | </pre> |
Revision as of 15:30, 7 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 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