A possible bug in python threading/time module?
Tim Peters
tim.one at comcast.net
Thu Jul 3 21:06:09 EDT 2003
[Tim]
>> If you run "enough" threads, chances are high you'll eventually run
>> into a platform thread bug on any platform -- but into different
>> platform bugs on different platforms. You didn't say which OS or
>> platform thread library you were using, and those are probably the
>> only things that matter.
>>
>> Here's a simplified and generalized minor rewrite of the above, with
>> a comment about what happens under Win2K and 2.3b2. I've got no
>> interest in chasing it down, since it's hung in the bowels of a
>> Windows DLL and there's no evidence of a Python bug:
>>
>> """
>> import time
>> import threading
>>
>> # One thread hung at the end on Win2K at N == 2011. # No problem if
>> N < 2011. N = 2011
>> sem = threading.Semaphore(N)
>>
>> class MyThread(threading.Thread):
>> def __init__(self):
>> threading.Thread.__init__(self)
>> sem.acquire()
>>
>> def run(self):
>> time.sleep(5)
>> sem.release()
>>
>> for i in range(2*N):
>> MyThread().start()
>> """
[vm_usenet]
> I tried it on Windows 2000 and XP.
"It" means the program above, or something else? What happened?
> I guess it could be a platform-specific bug, but it still disturbs me
> because I later programmed the same thing in C - and it worked
> perfectly.
Without seeing your C code I'm afraid this doesn't say anything -- "same
thing" has got to be stretching the truth in ways that can't be guessed.
> It's a long-shot, but I guess somewhere the bug is caused
> due to a misuse by the python runtime (or perhaps an inefficient use)
> of the platform-specific threading facilities...
Dig into it, then. All Python's synch gimmicks are built on the Python
lock, which is a cross-platform abstraction. On Windows, the implementation
of the Python lock is in Python/thread_nt.h. No bugs have been reported
against it, but that doesn't mean it doesn't have any. If you find one,
that would be great.
I expect there may be a bug *somewhere* in Python because a few thousand
threads is really far fewer than I expect to create trouble on Windows. The
evidence I saw suggested the program actually finished all its "useful"
work, and got hung in threading.py's _MainThread.__exitfunc(), waiting to
join one of the worker threads at Python shutdown time.
More information about the Python-list
mailing list