simple threading question
I started to approach multi threading, but I need something confirmed. For my testing purposes, I am using python 2.7.11 on Linux.
I put something really simple together. Two threads start at the same time and count to 9. One starts counting at 1 and the other one starts at 2.
import threading
class myThread(threading.Thread):
def __init__(self,threadId):
threading.Thread.__init__(self)
self.threadId=threadId
def run(self):
while self.threadId < 10:
print self.threadId
self.threadId+=1
thread1=myThread(1)
thread2=myThread(2)
thread1.start()
thread2.start()
My expectation was line by line counting to 9. The output sometimes looks like this:
1
22
3
34
5
46
7
58
6
9
7
8
9
Apparently, they can write in the same line while the other process is still going on? The line breaker does not always get called properly. Is this as expected?
For testing purposes I changed:
print self.threadId
to
print "cookies", self.threadId
Now the output looks like this:
cookiescookies 12
cookies 3cookies 2
cookies cookies 4
3cookies 5
cookies 4cookies
6cookies 5
cookies 7cookies 6
cookies 8cookies 7
cookies cookies 89
cookies 9
It looks like, we at do not overwrite "cookies". For my next test, I changed:
print "cookies", self.threadId
to
temp="cookies cake", self.threadId
print temp
The output is even more all over the place: (('cookies cake', 'cookies cake', 21))
('cookies cake'('cookies cake', , 3)2
)
('cookies cake'('cookies cake', , 43))
('cookies cake'('cookies cake', , 45))
('cookies cake'('cookies cake', 6)
, 5)
('cookies cake'('cookies c, ake'7)
('cookies cake', 8)
('cookies cake', 9)
, 6)
('cookies cake', 7)
('cookies cake', 8)
('cookies cake', 9)
Is this as expected? Because "duh this is multi threading?"
6 comments · 89% upvoted · other discussions
Some people, when confronted with a problem, think, "I know, I'll use threads," and then two they hav erpoblesms.