Difference between revisions of "Python threading"
From Noah.org
Jump to navigationJump to searchLine 3: | Line 3: | ||
== Basic threading example == | == Basic threading example == | ||
− | This | + | 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. | ||
Note that you might see "Done!" before the last line of thread output. | Note that you might see "Done!" before the last line of thread output. | ||
Line 12: | Line 12: | ||
import time | import time | ||
− | import threading | + | import threading |
− | class | + | 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 |
− | self.function = function | + | self.function = function |
− | self.args = args | + | self.args = args |
− | self.kwargs = kwargs | + | self.kwargs = kwargs |
− | self.finished = threading.Event() | + | self.finished = threading.Event() |
− | def cancel(self): | + | def cancel(self): |
− | self.finished.set() | + | self.finished.set() |
− | def run(self): | + | def run(self): |
− | while not self.finished.isSet(): | + | while not self.finished.isSet(): |
− | self.finished.wait(self.interval) | + | self.finished.wait(self.interval) |
self.function(*self.args, **self.kwargs) | self.function(*self.args, **self.kwargs) | ||
Line 34: | Line 34: | ||
print "Calling my_function() in a thread every 1/10th of second for two seconds." | 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) | time.sleep(2) | ||
− | + | t.cancel() | |
print "Done!" | print "Done!" | ||
</pre> | </pre> |
Revision as of 15:20, 7 June 2007
Basic threading example
This is a simple example of threading in Python. 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 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 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." t = thread_looper (0.1, my_function, (1,0,-1)) t.start() # The thread, t, runs while we are asleep. time.sleep(2) t.cancel() 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