Simplistic Python Thread example
Here is a simple Python example using the
Thread object in the
threading module.
import time
from threading import Thread
def myfunc(i):
print "sleeping 5 sec from thread %d" % i
time.sleep(5)
print "finished sleeping from thread %d" % i
for i in range(10):
t = Thread(target=myfunc, args=(i,))
t.start()
Results:
sleeping 5 sec from thread 0 sleeping 5 sec from thread 1 sleeping 5 sec from thread 2 sleeping 5 sec from thread 3 sleeping 5 sec from thread 4 sleeping 5 sec from thread 5 sleeping 5 sec from thread 6 sleeping 5 sec from thread 7 sleeping 5 sec from thread 8 sleeping 5 sec from thread 9
...and 5 seconds later:
finished sleeping from thread 0 finished sleeping from thread 1 finished sleeping from thread 2 finished sleeping from thread 3 finished sleeping from thread 4 finished sleeping from thread 5 finished sleeping from thread 6 finished sleeping from thread 7 finished sleeping from thread 8 finished sleeping from thread 9
6
Comments
—
Comments feed for this post
#3 Oleg Tarasenko commented on 2009-09-10:
I also like the example.
I tuned it a little bit, so can use like a very simple load testing tool. E.g. if you need to generate few thousands of request.
import urllib2
from threading import Thread
def btl_test(): while 1:
page = urllib2.urlopen("example.com")
for i in range(120):
t = Thread(target = btl_test)
t.start()
#5 Bino commented on 2009-12-09:
Hi Elliot and all .. That realy great impact example.
Just one question, at line -->
t = Thread(target=myfunc, args=(i,))
Why there is a "comma" after "i" while there is only single paramater ? is it a must ?
Sincerely -bino-
#6 Eliot commented on 2009-12-09:
Oleg,
That looks like a cool example. Thanks!
Bino,
I gave it a quick try, and it looks like the comma is required.
The Thread object expects a tuple for the args argument. Since the operator for forming tuples is the comma and not the parentheses, a comma is required for a tuple of one item. See here for more info about that.
Post a comment
About
I'm Eliot and this is my notepad for programming topics such as Python, Django, Ubuntu, Emacs, etc... more »
Search Blog
Tags
-
algorithms
(5)
-
aws
(9)
-
blogproject
(20)
-
c_cplusplus
(12)
-
cardstore
(8)
-
colinux
(2)
-
concurrency
(13)
-
conkeror
(2)
-
core
(2)
-
cygwin
(17)
-
datastructures
(14)
-
datetime
(4)
-
decorators
(4)
-
django
(40)
-
emacs
(22)
-
files_directories
(11)
-
git
(5)
-
hardware
(5)
-
install_setup
(8)
-
javascript
(3)
-
keyboard
(9)
-
matplotlib
(5)
-
mercurial
(4)
-
nginx
(2)
-
persistence
(5)
-
preferences
(7)
-
processes
(4)
-
pyqt
(18)
-
python
(144)
-
ratpoison
(3)
-
regexes
(6)
-
rsync
(3)
-
softwaretools
(17)
-
sql
(14)
-
ssh
(10)
-
subversion
(6)
-
twisted
(7)
-
ubuntu
(65)
-
urxvt
(5)
-
vxworks
(25)
-
webdev
(5)
-
wmii
(7)
Blogroll
- Adam Gomaa
- Alex Clemesha
- Amir Salihefendic
- Armin Ronacher
- David Beazley
- David Ziegler
- Duncan McGreggor
- Gareth Rushgrave
- Glyph Lefkowitz
- Guido van Rossum
- Ian Bicking
- Jacob Kaplan-Moss
- James Bennett
- James Tauber
- Jesper Noehr
- Marty Alchin
- Matt Harrison
- Nikolay Kolev
- Parand Darugar
- Peter Baumgartner
- Peter Bengtsson
- Rob Hudson
- Simon Willison
- Will McGugan
#1 Tomek commented on 2009-05-24:
Cool ;) I like such simplistic examples. THX.
Tomek