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

Tim Peters tim.peters at gmail.com
Wed May 3 17:46:38 EDT 2006


>> What do you mean "stop responding"?

[OlafMeding at gmail.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).



More information about the Python-list mailing list