threads blocked during import?
Mark Hammond
MarkH at ActiveState.com
Tue Apr 10 20:55:36 EDT 2001
Geoff Talvola wrote:
> I was wondering the other day: if two threads try to import the same
> module at the same time, what happens? I wrote a test program, and it
> looks like an import blocks all other threads from importing. This is
> probably a good thing, since you only want the module to be imported once.
>
> Here's the test program, and the helper module that it imports. Running
> it on my WinNT box shows that the 2nd thread to try to import the module
> blocks while the first thread is importing it, and the module doesn't
> get imported twice. That's a good thing.
>
> My question for the gurus is, can I count on this locking taking place,
> and put code that absolutely needs to run only once at module level
> without any special locking?
Yes. The Python win32com support actually discovered Python had a bug
in this area, so Guido added an explicit lock just for this case.
Mark.
More information about the Python-list
mailing list