Malcolm Tredinnick wrote:
> 
> On Mon, 2008-07-14 at 23:30 -0700, django-m712 wrote:
> [...]
>> Has anyone else experienced this problem?
> 
> There are a few places in Django where we need to access all the models
> in an app, for example. So we use __import__(), since that's the safest
> way to do this. This is the same as calling reload() from the Python
> interactive prompt if the module is already imported.
> 
> In this case, what you're seeing is probably
> django.db.models.loading.get_apps() doing its thing, but there are other
> places it can happen as well.

I might be misunderstanding you, but I don't think that's the case in 
general. For instance, if I create module.py like this:

   print 'Module loaded'

And loader.py like this:

   import module
   __import__('module')

Then, when I run loader.py, "Module loaded" is printed only once.

However, one way that a module *can* be loaded twice is if it's part of 
a package and is imported using two different "paths". For instance, if 
both module.py and loader.py above are part of a package named 
"package", and PYTHONPATH is set up correctly, and loader.py looks like 
this:


   import module #This loads module once
   import package.module #This loads it again

Then "Module loaded" is printed twice.


>> Is there a way to fix this?
> 
> The premise there is that it's something that needs to be fixed. :-)
> 
> Modules can be imported multiple times. That's just a fact of life. It
> can happen both through the use of __import__ and because of things like
> an Apache process restarting and thus it needs to load modules (which
> isn't the same as reloading into the same process space, but if you're
> doing something which should only happen once externally, you need to
> take measures).
> 
> For better or worse, your code needs to handle the possibility that
> things (app packages, model modules, URL config files) could be imported
> multiple times.

I agree with all this, however. Unless you control all the places where 
a module is imported, it has to be ready to be loaded more than once.

Mike

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to