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
Comments
Cool ;) I like such simplistic examples. THX.
Tomek
Tomek, heh, sometimes I do too.
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()
Nice ... ssssssssssss very useful
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-
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.
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-
bino, the ',' after is necessary here since the argument (or the set of arguments) is a tuple. remember (tuple,) [list,] {dictionary,}
in the above types, a comma is not mandated after the last element unless it only has a single element. So, you would not have to type list = [1,2,] but you would have to type list = [1,] or it would not be a list. Why make a list with one element? Well, you could always append to a list iff it IS a list.
ps. The reason I chose list instead of tuple is that the word list seems to naturally make sense in this explanation.
I finally broke in to that exclusive world of threads, thanks to your example. THANKS A TONNE !!!!!!!!
khanal: The comma is only required for tuples. Single item lists and dicts don't require a comma.
very useful example thanks a lot...................
very helpful..
what happen if i get this kind of things :
jsb.lib.threads:98 start_new_thread | .usr.lib.python2.7.threading:495 start | <class 'thread.error'>: can't start new thread
threads - thread space is exhausted - can't start thread
Dear sir, I am new to python, and i tried several examples , now i am looking for something in threading and multithreading, i already search on web but only found how to print thread number and timing of thread execution, i am looking for some thing where i can use thread with my own programs and also some basics of threading with details. Thank you in advance. Regards
#9 Eliot commented on 2012-03-30:
khanal: The comma is only required for tuples. Single item lists and dicts don't require a comma.
Note: parenthesis are also for grouping Boolean expressions and math.
groovy. but on my setup the 'finished' notifications came crashing back all jumbled up. some of them on the same line :o
i put a tiny delay in the for loop and it organized them to all arrive back in order like good little boys and girls:)
Really love this kind of small examples to get things started~~ Thanks a lot!
BTW: when I tried the code, the "finished results" are not nicely printed in order, kind of in a random way like:
finished thread 0
finished thread 2
finished thread 1finished thread 3
finished thread 7
finished thread 4
finished thread 5
finished thread 9finished thread 6finished thread 8
Salty alright :)
disqus:1937590802
Wow! Cool!
disqus:2329680709
Thank you! my threading simply stopped working and I was waiting for one render node to complete before I could start the others.
Following your simple syntax fixed the problem.
disqus:2925082283
Thank you!!
disqus:3062177352
Hi there, is there a way to replace the "target" with a variable so I can control which function to call? For example (target=[variable].... where [variable] = some function name?
disqus:3447459350