473,729 Members | 2,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.Threa d.__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_ne w_thread(run, (1, 0.01))
thread.start_ne w_thread(run, (2, 0.003))

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

May 3 '06 #1
17 6426
Ol********@gmai l.com wrote:
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.


What do you mean "stop responding"? Not responding when you press
ctrl-c? They stop printing? If you mean stop printing, try
sys.stdout.flus h() after each print

May 3 '06 #2
[Ol********@gmai l.com]
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.Threa d.__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>pyt hon 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.0004793495332 38
0.0005219042829 16
0.0005636490373 59

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_ne w_thread(run, (1, 0.01))
thread.start_ne w_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()


Works much the same for me, except I got bored quicker ;-):

C:\Python24>pyt hon test2..py
thread 1 started
sleep time: 0.01
1.14999323533e-006
8.01271757225e-005
thread 2 started
sleep time: 0.003
0.0003958653184 39
0.0004742598572 95
0.0005598317068 72

0.0007106134669 8

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.
May 3 '06 #3
> 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

May 3 '06 #4
Time
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


This is exactly what you should see. The problem I see is that after a
while (minutes to hours) the printing of 1s and 2s stops! If you press
ctrl-c at that point the threads will print how many seconds they were
stuck in the sleep statements (proving that the threads were stuck in
the sleep statement until you pressed ctrl-c).

Could you please try again (perhaps let it run overnight)? Many, many
thanks.

Olaf

May 3 '06 #5
Tim
Do you want someone running this test to hit the ENTER key, or not?


The purpose of the "sys.stdin.read (1)" statement is simply to prevent
the main thread from exiting and thus ending the test. And yes, I also
get an exception when I press the enter key.

Olaf

May 3 '06 #6

Ol********@gmai l.com wrote:
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?


I don't have such CPU but I run the first program anyway. It printed

C:\py>th.py
thread 1 started
sleep time: 0.01
3.63174649292e-006
8.43682646817e-005
0.0001648254177 56

thread 2 started
sleep time: 0.003
0.0006752254825 68
0.0007534477147 24
0.0008294350259 6

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

I got bored and tried to stop it with ctrl-c but it didn't respond and
kept running and printing the numbers. I had to kill it from task
manager.

May 3 '06 #7
Serge
I got bored and tried to stop it with ctrl-c ...


Yes, you have to use the ctrl-break key to stop the first program. And
neither program every hangs on a single core CPU. It also does not
hang on a hyper-threading CPU if hyper-threading is turned off in the
BIOS.

Olaf

May 3 '06 #8
>> What do you mean "stop responding"?

[Ol********@gmai l.com]
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!
Where "hang" means they stop printing.
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?


I was using a 3.4 GHz Pentium 4 (single core) with hyper-threading
enabled. I didn't run it for hours ;-)

But supposing I do and see a hang, it's unlikely that will have
anything to do with Python. On Windows, time.sleep() called from any
thread other than the main thread just calls the Win32 API Sleep()
function, after converting the argument to milliseconds. So it may be
more fruitful to recode your test program in C (if there is a bug
here, it's most likely in Microsoft's implementation of its Sleep()
function).
May 3 '06 #9
Tim
I didn't run it for hours ;-)
Please try.

The sleep statement does not return! And this should not happen. The
code above does nothing special or unusual. The problem only occurs if
2 threads use the sleep statement and hyper-threading is enabled.

We discovered this bug perhaps a year ago. The only solution was to
tell our customers to disable hyper-threading (you can imagine they did
not like our "solution" very much). It then took many days of hard
work to isolate the problem down to the code I posted above.
Where "hang" means they stop printing.


Our Python code just stops running (locking up the entire application).
We use Qt for our GUI. We have over a hundred .py files. We use a
total of 4 threads (they get created once and stay running). One
thread uses sockets.

Once the application locks up (getting stuck in a sleep statement) all
comes back to live if I pull the network cable out. So perhaps the
socket thread returns from the sleep statement and other threads return
to live because they were waiting for the socket thread.

Our software runs on both Windows and Linux. We are not sure if the
problem also happens on Linux.

In any case, if someone else can confirm the bug then this is a serious
problem meriting further investigation.

We have searched the Internet far and wide and were not able to find
any information that indicates that someone else has reported a similar
problem (neither Python nor Windows Sleep related).

Thank you for your help.

Olaf

May 4 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4030
by: bill ramsay | last post by:
Dear all. I am using an existing hodge-podge of an application that runs on top of an Access database. This application dials up customer equipment, handshakes then downloads/uploads various bits of information in the access database. The original application is designed for some poor sod to sit there for hours and manually do the necessary.
29
3227
by: Jeffrey Maitland | last post by:
Hello all, I am in the process of writing a multithreading program and what I was wondering is a sleep command in an executing function will affect the threads below it? Here is a basic example of what I mean. def main(): temp_var = True while temp_var == True: if
5
9097
by: Erich Schreiber | last post by:
In the Python Library Reference the explanation of the time.sleep() function reads amongst others: > The actual suspension time may be less than that requested because > any caught signal will terminate the sleep() following execution > of that signal's catching routine. Also, the suspension time may > be longer than requested by an arbitrary amount because of the > scheduling of other activity in the system. I don't understand the...
1
2802
by: Yin | last post by:
I am writing a script that monitors a child process. If the child process dies on its own, then the parent continues on. If the child process is still alive after a timeout period, the parent will kill the child process. Enclosed is a snippet of the code I have written. For some reason, unless I put in two time.sleep(4) commands in the parent, the process will not sleep. Am I forgetting something? Any reasons for this strange...
5
23355
by: Parahat Melayev | last post by:
I am trying to writa a multi-client & multi-threaded TCP server. There is a thread pool. Each thread in the pool will handle requests of multiple clients. But here I have a problem. I find a solution but it is not how it must be... i think. When threads working without sleep(1) I can't get response from server but when I put sleep(1) in thread function as you will see in code, everything works fine and server can make echo nearly for...
3
2116
by: kiplring | last post by:
Suppose a function which has Sleep() method in it. And I want to recycle it. I made two buttons which call "Resume()" and "Suspend()". But It doesn't work. The state of thread "t" will be "WaitSleepJoin" when it runs because the function it calls - "GenerateEvents()" has "Sleep()" function. Can I solve this problem with Thread? I don't want add nasty boll flags on it.
5
2584
by: mrkbrndck | last post by:
Please see the code below as I am trying to use multithreading for copying files to a new location in a way that improves performance of the client windows application. The problem occurs when 2 or more threads are created, the ImportOneFile method attempts to add a previously added file. If I allow 4 maximum threads and process 4 files, the last file is attempted 4 times and none of the other files are added to the destination. If I...
18
9816
by: barry | last post by:
I'm having a problem creating a delay before each list item gets moved ( http://www.polisource.com/PublicMisc/list-splitter-timeout-bug.html ). Choose any number of columns, vertical layout, and view as a web page. They'll be a small delay before the entire bunch of list items converts from left-to-right to top-to-bottom order. I expected a delay before each item is moved. It looks like I have to flush the buffer to get setTimeout to work,...
4
11070
by: Frankie | last post by:
This is from MSDN online (http://msdn2.microsoft.com/en-us/library/d00bd51t.aspx): "Specify zero (0) to indicate that this thread should be suspended to allow other waiting threads to execute." My questions: 1. By "other threads" - does the above statement refer specifically and only to other threads in the current AppDomain?
1
2736
by: danfolkes | last post by:
Hey Everyone, I am trying to send repeated messages from a "Node" to a "Server". It works the first time I send the from the Node to Server, but after that it either errors, or does not do anything. I would love some help, here is the code: import socket import thread import time
0
8913
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9280
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2162
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.