问题描述
以下是隔离问题的2个文件。请注意,两个程序都挂起了超级线程(BIOS设置),然后
按预期工作,超线程关闭。
注意,Windows任务管理器在性能选项卡上显示2个CPU,并打开了
超线程。
Python 2.3.5和2.4.3(从python.org下载)都有这个问题。
操作系统是MS Windows XP Professional。
winmsd.exe显示:
2CPU:x86 Family 15 Model 4 Stepping 1 GenuineIntel~3000 MHz
版本:5.1.2600 Service Pack 2构建2600
有超级线程(或双/多核)CPU的人请
确认这个错误吗?
非常感谢
奥拉夫
#testsleep.py
导入线程
导入时间
class Task(threading.Thread):
def __init __(self,n,t):
threading.Thread。 __init __(S精灵)
self.n = n
self.t = t
def run(self):
print''thread%d started''%self.n
print''sleep time:'',self.t
print time.clock()
打印time.clock()
打印time.clock()
打印
count = 0
printCount = int(10 / self.t)
而True:
start = time.clock()
time.sleep (self.t)
stop = time.clock()
if stop - start> 1.0:
打印''线程'',self.n,停止 - 开始
count + = 1
if count > printCount:
count = 0
print self.n,
def test():
thread1 =任务(1,0.01)
thread2 =任务(2,0.003)
thread1.start()
thread2.start( )
test()
---------------------- --------------------------------------------------
#testsleep2.py
导入线程
导入时间
导入系统
def run(n,t):
打印''线程%d开始''%n
打印''睡眠时间:' ',t
打印time.clock()
打印time.clock()
打印time.clock()
打印
count = 0
printCount = int(10 / t)
而True:
start = time.clock()
time.sleep(t)
stop = time.clock()
if stop - start> 1.0:
打印''线程'',n,停止 - 开始
count + = 1
if count> printCount:
count = 0
打印n,
def test():
线程.start_new_thread(run,(1,0.01))
thread.start_new_thread(run,(2,0.003))
#等到用户按下输入密钥。
sys.stdin.read(1)
test()
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()
推荐答案
您是什么意思停止响应?当你按
ctrl-c时没响应?他们停止印刷?如果您的意思是停止打印,请在每次打印后尝试
sys.stdout.flush()
What do you mean "stop responding"? Not responding when you press
ctrl-c? They stop printing? If you mean stop printing, try
sys.stdout.flush() after each print
工作原理大致相同对我来说,除了我感到无聊更快;-):
C:\ Python24> python test2..py
线程1开始
睡眠时间:0.01
1.14999323533e-006
8.01271757225e-005
线程2开始
睡眠时间:0.003
0.000395865318439
0.000474259857295
0.000559831706872
0.00071061346698
1 2 1 2 1 2 1 2 1 1 2 1 2 1 1 2 1 1 1
此时我按下ENTER键,然后看到了:
线程中未处理的异常由
开始
sys.excepthook中的错误:
原始异常是:
未处理的异常在线程开始由
sys.excepthook中的错误:
原始异常是:
那是不幸的,但不是意外的。解释器
并不等待`thread`模块线程完成之前撕下
本身,所以线程在Python撕裂之后继续运行
本身就会发生奇怪的异常 - 而且Python已经被破坏了
这一点它甚至无法显示有用的错误消息。
解释器(默认情况下)等待`threading`模块线程
退出然后撕下来,所以那些无用的退出
在第一次测试中没有预期的消息。
Works much the same for me, except I got bored quicker ;-):
C:\Python24>python test2..py
thread 1 started
sleep time: 0.01
1.14999323533e-006
8.01271757225e-005
thread 2 started
sleep time: 0.003
0.000395865318439
0.000474259857295
0.000559831706872
0.00071061346698
1 2 1 2 1 2 1 2 1 1 2 1 2 1 2 1 2 1 2 1
At that point I hit the ENTER key, and saw:
Unhandled exception in thread started by
Error in sys.excepthook:
Original exception was:
Unhandled exception in thread started by
Error in sys.excepthook:
Original exception was:
That''s unfortunate, but not unexpected either. The interpreter
doesn''t wait for a `thread` module thread to finish before tearing
itself down, so the threads keep running after Python has torn so much
of itself down that weird execptions occur -- and Python is _so_ torn
down by that point it can''t even display a useful error message. The
interpreter does (by default) wait for `threading` module threads to
exit before tearing itself down, so those kinds of useless exit
messages weren''t expected in the first test.
这篇关于在2个线程中使用time.sleep()会在启用超线程时导致锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!