Difference between revisions of "Python threading"
From Noah.org
Jump to navigationJump to search(9 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
[[Category:Python]] | [[Category:Python]] | ||
[[Category:Free_Software]] | [[Category:Free_Software]] | ||
− | |||
== Basic threading example == | == Basic threading example == | ||
This is a simple example of threading in Python. | This is a simple example of threading in Python. | ||
− | + | You pass it any function and your function is run in a thread loop. | |
− | <include svncat src="/home/svn/src/python/thread_looper.py" highlight="python" /> | + | Click to download: [http://www.noah.org/downloadsvn.php?src=file:///home/svn/src/python/thread_looper.py thread_looper.py] |
+ | <include svncat src="file:///home/svn/src/python/thread_looper.py" highlight="python" /> | ||
− | + | 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