simple Thread question

adeger adeger at netlibrary.com
Tue Aug 3 21:51:32 CEST 2004


Having trouble with my first forays into threads.  Basically, the
threads don't seem to be working in parallel (or you might say are
blocking).  I've boiled my problems to the following short code block
and ensuing output.  Seems like the output should be all interleaved
and of course it's not.  Running Python 2.2 from ActiveState on
Windows XP (also doesn't work on Windows 2000).

Thanks in advance!
adeger

#====================================================

import threading

class TestThr(threading.Thread):
	def __init__(self):
		threading.Thread.__init__(self)

	def run(self, name):
		import time
		for i in range(1,11):
			print 'thread ', name, ' instance ', str(i)
			time.sleep(1)

threads = []
for inst in ('a', 'b', 'c'):
	thread = TestThr()
	thread.run(inst)
	threads.append(thread)

# output below
thread  a  instance  1
thread  a  instance  2
thread  a  instance  3
thread  a  instance  4
thread  a  instance  5
thread  a  instance  6
thread  a  instance  7
thread  a  instance  8
thread  a  instance  9
thread  a  instance  10
thread  b  instance  1
thread  b  instance  2
thread  b  instance  3
thread  b  instance  4
thread  b  instance  5
thread  b  instance  6
thread  b  instance  7
thread  b  instance  8
thread  b  instance  9
thread  b  instance  10
thread  c  instance  1
thread  c  instance  2
thread  c  instance  3
thread  c  instance  4
thread  c  instance  5
thread  c  instance  6
thread  c  instance  7
thread  c  instance  8
thread  c  instance  9
thread  c  instance  10



More information about the Python-list mailing list