Nick Coghlan added the comment:
Perhaps an API like importlib.util.locks_held() that returns a list of module
names?
We'd then just iterate over the _module_locks() dictionary, looking for locks
where the owner matched the current thread id (alternatively, if speed was
critical for Guido's
Eric Snow added the comment:
(Unless I've missed something, we don't run user code with the global
import lock held any more)
Ah. You are correct.
--
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23068
Guido van Rossum added the comment:
Oh. I'd forgotten the PY3 situation is completely different from the PY2
situation. We're still stuck here in PY2 land where there's just the one
import lock. I guess not even ctypes can help us find out whether the
current thread is holding the import lock
Nick Coghlan added the comment:
Looking at the implementation of PyImport_ImportModuleNoBlock, you should be
able to invoke that via ctypes with a nonsense module name to probe for whether
or not the current thread has the import lock.
A call like
Nick Coghlan added the comment:
You'd still need to check lock_held() to see if *some* thread is holding the
import lock. The non-blocking import API should then let you determine if that
thread is the current one.
--
___
Python tracker
Guido van Rossum added the comment:
Interesting. The Dropbox server team thanks you for the suggestion!
--
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue23068
___
New submission from Guido van Rossum:
imp.lock_held() returns True if *any* thread is currently importing something.
I'd like to have an API that can tell whether the *current* thread has the
import lock. The implementation keeps track of this, but doesn't make the info
available.
Use case:
Eric Snow added the comment:
Keep in mind that the global import lock is only held long enough to create a
module-level lock. The cache of module locks is found at line 166 of
Lib/importlib/_bootstrap.py and the code related to module-level locking
follows. So unfortunately it won't be as