[C++-sig] Calling import() multiple times for the same module

Stefan Seefeld seefeld at sympatico.ca
Fri Oct 24 20:07:30 CEST 2008


Robert Dailey wrote:
> Hi,
>
> What happens if I do the following?
>
> using namespace boost::python;
>
> import( "__main__" ).attr( "new_global" ) = 40.0f;
> import( "__main__" ).attr( "another_global" ) = 100.0f:
>
>
> My main concern here is performance. I'm wondering if each call to
> import() results in a disk query for the script in question and loads it
> from there. I'm also wondering if the second import() above will simply
> read from a memory cache or something.

As the above code is only a wrapper around the Python runtime, you are 
really asking about how Python handles repeated 'import' calls. I'm 
pretty sure importing a module while it is already imported will do 
(almost) nothing. However, I'm not sure whether a module is actually 
unloaded as soon as the last reference goes away (such as after the 
first line above is completed), so I can't give a definite answer.

I'm sure you'll get better answers when asking on a Python forum directly.

Regards,
       Stefan

-- 

      ...ich hab' noch einen Koffer in Berlin...



More information about the Cplusplus-sig mailing list