Thread-safe atomic operation

Gordon McMillan gmcm at hypernet.com
Tue Apr 11 16:49:37 EDT 2000


Aahz Maruch wrote:

> In article <1256659126-41087547 at hypernet.com>,
> Gordon McMillan <gmcm at hypernet.com> wrote:
> >Dong-gweon Oh writes:
> >>
> >> Are these statements thread-safe?
> >> 1. import xxx
> >> 2. xxx = __import__('xxx')
> >> 3. reload(xxx)
> >> 4. dict['xxx'] = sys.modules['xxx']
> >> 5. dict.has_key('xxx')
> >
> >Numbers 2 and 4 could be interrupted between evaluation of 
> >the RHS and assigment to LHS. For #2 that would be of 
> >concern only if xxx was a global.
> 
> Are you absolutely certain about item 1?  That implies that Python as a
> whole could block on importing a module that did something like
> 
> for i in range(100):
>   sleep(1)

I didn't say it was atomic, I agreed it was thread safe. There's 
special code in there dealing with competing imports between 
threads, but the global lock is not held for the duration of the 
import (in particular, the top level Python code in the imported 
module executes according to the normal rules).

- Gordon




More information about the Python-list mailing list