Difference between revisions of "Python threading"

From Noah.org
Jump to navigationJump to search
 
(14 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[Category:Engineering]]
 
[[Category:Engineering]]
 
[[Category:Python]]
 
[[Category:Python]]
 +
[[Category:Free_Software]]
 
== Basic threading example ==
 
== Basic threading example ==
  
This gives a simple example of threading in Python.
+
This is a simple example of threading in Python.
This runs my_function() in a thread loop.
+
You pass it any function and your function is run in a thread loop.
Note that you might see "Done!" before the last line of thread output.
 
Welcome to threadland!
 
  
<pre>
+
Click to download: [http://www.noah.org/downloadsvn.php?src=file:///home/svn/src/python/thread_looper.py thread_looper.py]
#!/usr/bin/env python
+
<include svncat src="file:///home/svn/src/python/thread_looper.py" highlight="python" />
  
import time
+
Example output:
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!"
 
</pre>
 
 
Example output:
 
 
<pre>
 
<pre>
 
Calling my_function() in a thread every 1/10th of second for two seconds.
 
Calling my_function() in a thread every 1/10th of second for two seconds.

Latest revision as of 19:00, 3 January 2008

Basic threading example

This is a simple example of threading in Python. You pass it any function and your function is run in a thread loop.

Click to download: thread_looper.py <include svncat src="file:///home/svn/src/python/thread_looper.py" highlight="python" />

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