I am trying to use threads in a Python project I am working on, but threads don't appear to be behaving as they are supposed to in my code. It seems that all threads run sequentially (i.e. thread2 starts after thread 1 ends, they don't both start at the same time). I wrote a simple script to test this, and that too runs threads sequentially.
import threading
def something():
for i in xrange(10):
print "Hello"
def my_thing():
for i in xrange(10):
print "world"
threading.Thread(target=something).start()
threading.Thread(target=my_thing).start()
Here's the output I get from running it:
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
world
world
world
world
world
world
world
world
world
world
The same behavior is observed with much larger number of iterations of the loops.
I tried searching the web and older SO answers, but I couldn't find anything that helped.
Can someone please point out what is wrong with this code?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…