Using time.sleep() in 2 threads causes lockup when hyper-threading is enabled

OlafMeding at gmail.com OlafMeding at gmail.com
Wed May 3 14:42:08 EDT 2006


Below are 2 files that isolate the problem.  Note, both programs hang
(stop responding) with hyper-threading turned on (a BIOS setting), but
work as expected with hyper-threading turned off.

Note, the Windows task manager shows 2 CPUs on the Performance tab with
hyper-threading is turned on.

Both Python 2.3.5 and 2.4.3 (downloaded from python.org) have this
problem.
The operating system is MS Windows XP Professional.

winmsd.exe shows:
2CPUs: x86 Family 15 Model 4 Stepping 1 GenuineIntel ~3000 MHz
Version: 5.1.2600 Service Pack 2 Build 2600

Could someone with a hyper-threading (or dual/multicore) CPU please
confirm this bug?

Many Thanks

Olaf


# testsleep.py
import threading
import time

class Task(threading.Thread):
	def __init__(self, n, t):
		threading.Thread.__init__(self)
		self.n = n
		self.t = t
	def run(self):
		print 'thread %d started' % self.n
		print 'sleep time:', self.t
		print time.clock()
		print time.clock()
		print time.clock()
		print
		count = 0
		printCount = int(10 / self.t)
		while True:
			start = time.clock()
			time.sleep(self.t)
			stop = time.clock()
			if stop - start > 1.0:
				print 'thread', self.n, stop - start

			count += 1
			if count > printCount:
				count = 0
				print self.n,

def test():
	thread1 = Task(1, 0.01)
	thread2 = Task(2, 0.003)
	thread1.start()
	thread2.start()

test()

------------------------------------------------------------------------

# testsleep2.py
import thread
import time
import sys

def run(n, t):
	print 'thread %d started' % n
	print 'sleep time:', t
	print time.clock()
	print time.clock()
	print time.clock()
	print
	count = 0
	printCount = int(10 / t)
	while True:
		start = time.clock()
		time.sleep(t)
		stop = time.clock()
		if stop - start > 1.0:
			print 'thread', n, stop - start

		count += 1
		if count > printCount:
			count = 0
			print n,

def test():
	thread.start_new_thread(run, (1, 0.01))
	thread.start_new_thread(run, (2, 0.003))

	# Wait until the user presses the enter key.
	sys.stdin.read(1)
	
	
test()




More information about the Python-list mailing list