[Python-Dev] Import lock considered mysterious

Nick Coghlan ncoghlan at gmail.com
Fri Jul 22 16:24:20 CEST 2011


On Fri, Jul 22, 2011 at 7:29 PM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Is it really necessary to hold the import lock for so long?
> Presumably the import lock is there to protect access to
> things like sys.modules. Is that all? Could it be released
> after the module code is loaded and sys.modules has been
> updated, but before executing the module body?

No, because we don't know if we're keeping it until the module body is
executed successfully and we don't want other threads to see the
partially initialised module.

There's a reason the docs say never to spawn and then wait for threads
as a side effect of import
(http://docs.python.org/library/threading#importing-in-threaded-code)

If you want to delegate main module execution to another file, then
runpy.run_module is available.

Antoine's fine grained import locking patch (or something along those
lines) would improve matters, but currently causes its own deadlock
problems.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list