在2个线程中使用time.sleep()会在启用超线程时导致锁定 [英] Using time.sleep() in 2 threads causes lockup when hyper-threading is enabled

查看:121
本文介绍了在2个线程中使用time.sleep()会在启用超线程时导致锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是隔离问题的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()

推荐答案

Ol********@gmail.com 写道:
下面是2个隔离问题的文件。请注意,两个程序都会挂起(停止响应)并启用超线程(BIOS设置),但
按预期工作,超线程关闭。
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.




您是什么意思停止响应?当你按

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


[Ol **** **** @ gmail.com]
[Ol********@gmail.com]
下面是2个隔离问题的文件。注意,两个程序都挂起
(停止响应)


什么是停止响应打开超线程(BIOS设置)意味着?b / b
,但是超线程关闭后工作正常。

请注意,Windows任务管理器在性能选项卡上显示2个CPU,并打开超线程。

Python 2.3.5和2.4.3(从python.org下载)都有这个
问题。
操作系统是MS Windows XP Professional。

winmsd.exe显示:
2CPU:x86系列15型号4步进1 GenuineIntel~3000 MHz
版本:5.1.2600 Service Pack 2 Build 2600


具有超线程(或双/多核)CPU的人是否可以确认此错误?


我在使用2.4.3启用HT

的WinXP Pro SP2盒子上看不到任何意外(对我而言),但我' '我不确定是什么'没有回应意思是

你。

#testsleep.py
导入线程
导入时间

类任务(threading.Thread ):
def __init __(self,n,t):
threading.Thread .__ init __(self)
self.n = n
self.t = t
def run(self):
print''trread%d started''%self.n
print''sleep time:'',self.t
print time.clock()<打印time.clock()
打印time.clock()
打印
count = 0
printCount = int(10 / self.t)
while是的:
start = time.clock()
time.sleep(self.t)
stop = time.clock()
如果停止 - 启动> 1.0:
打印''线程'',self.n,停止 - 开始


你没有_expect_这个打印执行,是吗?它很少会触发

(如果有的话)。

count + = 1
如果计数> printCount:
count = 0
打印self.n,
def test():
thread1 =任务(1,0.01)
thread2 =任务(2,0.003)
thread1.start()
thread2.start()

test()


这就是发生的事当我跑的时候,直到我厌倦了看它

并杀死了这份工作:


C:\Python24> python test1.py

线程1开始

睡眠时间:0.01

7.8499538238e-007

4.42770924877e-005

8.62618455186e-005

线程2开始

睡眠时间:0.003

0.000479349533238

0.000521904282916

0.000563649037359

1 2 1 2 1 2 1 2 1 1 2 1 2 1 2 1 2 1 2 1 1 2 1 2 1 >
2 1 2 1 2 1 1 2 1 2 1 2 1 2 1 1 2 1 2 1 1 2 1 2

1 1 2 1 2 1 2 1 2 1 2 1 1 2 1 2 1 2 1 2 1

#testsleep2.py
导入帖子
impo rt time
导入sys

def run(n,t):
打印''线程%d开始''%n
打印''睡眠时间:' ',t
打印time.clock()打印time.clock()
打印time.clock()
打印
count = 0
printCount = int(10 / t)
而真:
start = time.clock()
time.sleep(t)
stop = time.clock()
如果停止 - 开始> 1.0:
打印''线程'',n,停止 - 开始


见上文。

count + = 1
如果计数> printCount:
count = 0
打印n,

def test():
thread.start_new_thread(运行,(1,0.01))
线程.start_new_thread(run,(2,0.003))

#等到用户按下回车键。
sys.stdin.read(1)


您是否希望运行此测试的人按ENTER键?

test()
Below are 2 files that isolate the problem. Note, both programs hang
(stop responding)
What does "stop responding" mean?
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?
I don''t see anything unexpected (to me) on a WinXP Pro SP2 box with HT
enabled, using 2.4.3, but I''m not sure what "not responding" means to
you.
# 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
You don''t _expect_ this print to execute, do you? It should trigger
very rarely (if ever).
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()
This is what happened when I ran it, until I got tired of watching it
and killed the job:

C:\Python24>python test1.py
thread 1 started
sleep time: 0.01
7.8499538238e-007
4.42770924877e-005
8.62618455186e-005

thread 2 started
sleep time: 0.003
0.000479349533238
0.000521904282916
0.000563649037359

1 2 1 2 1 2 1 2 1 1 2 1 2 1 2 1 2 1 2 1 1 2 1 2 1
2 1 2 1 2 1 1 2 1 2 1 2 1 2 1 1 2 1 2 1 2 1 2 1 2
1 1 2 1 2 1 2 1 2 1 2 1 1 2 1 2 1 2 1 2 1
# 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
See above.
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)
Do you want someone running this test to hit the ENTER key, or not?

test()




工作原理大致相同对我来说,除了我感到无聊更快;-):


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.


>你是什​​么意思停止响应?


两个线程每隔10秒打印一次大约

的线号(1或2)。但是,过了一会儿(几分钟到几个小时)两个

程序(见上文)挂了!


按ctrl-c(打印停止后)会导致线程唤醒

up从他们的睡眠声明。由于睡眠花费超过1

秒,因此线程数和睡眠持续时间将打印到屏幕上。


你有超线程/双核/多核CPU吗?你试过这个吗?


奥拉夫

> What do you mean "stop responding"?

Both threads print their thread numbers (either 1 or 2) approximately
every 10 seconds. However, after a while (minutes to hours) both
programs (see above) hang!

Pressing ctrl-c (after the printing stops) causes the threads to "wake
up" from their sleep statement. And since the sleep took more than 1
seconds the thread number and the duration of the sleep is printed to
the screen.

Do you have a hyper-threading/dual/multi core CPU? Did you try this?

Olaf


这篇关于在2个线程中使用time.sleep()会在启用超线程时导致锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆