I am not sure this is important or now, but reload() (imp.reload() in 3.x) doesn't take the import lock when reloading: $ echo 'import imp; print("lock held =", imp.lock_held())' > foo.py $ ./python -c 'import imp, foo; imp.reload(foo)' lock held = True lock held = False
So the import lock is to prevent trying to import the same module, right? If you are doing a reload, the module is basically already there. But what if you context switch while reloading? That would be bad as that would give you inconsistent state. So it probably should hold the lock (unless I am off about what the import lock should truly be used for).
I just ran Antoine's test against the latest 3.3a4 build and received this: lock held = False lock held = False Is this issue still relevant given Brett's work on re-implementing import in pure Python?
Yeah, imp.reload() is pure Python now (Lib/imp.py), a simple wrapper around module.__loader__.load_module(), which does not make use of any import locks. Having it do so, however, may not be feasible as I'd expect it to mean having load_module() handle the locking. Not sure if it's worth it. The other issue is that the True reported by Antoine turned into False for you under 3.3a4. This is because of issue 9260 ("A finer grained import lock"). The global import lock represented in the imp module is now used only long enough to create a lock specific to the module currently being imported. So it's been released by the time the module is actually imported. (see Lib/importlib/_bootstrap.py)