I’ve been trying to get my head around Python’s treading feature for a while now but can’t get it to work how I expected it to (which is clearly a wrong understanding).
from threading import Thread
from time import sleep
def firstShout():
print "Hello"
def distantEcho():
sleep(5)
print "World"
t1 = Thread(target=distantEcho())
t2 = Thread(target=firstShout())
t1.start()
t2.start()
I would expect this to open the first thread, which sleeps for 5 seconds whilst also calling the second thread which will instantly print “Hello”. So the desired output would be “Hello” (5 second sleep) “World”. Instead, I’m getting (5 second sleep) “World” “Hello”, as if I’d not used threading at all.
As stated, this is clearly my lack of understanding of the threading feature, but I’d like to see how to get it to work with this test.
Simply remove calls (brackets) in your threads definitions: