Opened 14 years ago

Closed 13 years ago

Last modified 8 years ago

#14606 closed Uncategorized (invalid)

Application module imported twice when running development server

Reported by: rlz Owned by: nobody
Component: Core (Other) Version: 1.2
Severity: Normal Keywords: runserver
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I use Django 1.2.3.

I've created a new django project testproject and a new django application test.
I've put the allication inside settings.py, then put only one line of code in test/init.py:

print 'loaded'

And import the applicaton module from application urls.py:

import test

In result the line 'loaded' was printed twice.

This behaviour lead to error in my real project.

Change History (10)

comment:1 by Keryn Knight <keryn@…>, 14 years ago

Is this not a function of the project/application being loaded once per process?

What environment are you experiencing this under? manage.py runserver or perhaps a mod_wsgi setup?

in reply to:  1 comment:2 by anonymous, 14 years ago

Replying to Keryn Knight <keryn@kerynknight.com>:

Is this not a function of the project/application being loaded once per process?

What environment are you experiencing this under? manage.py runserver or perhaps a mod_wsgi setup?

I used manage.py runserver

comment:4 by Natalia Bidart, 14 years ago

Status: newassigned

Confirmed the double import. After some simple, I can confirm that manage.py:execute_manager() is called twice.

The re-entrance point is located within the Command implementation for runserver:

django/core/management/commands/runserver.py(99)

where the autoreload.main(inner_run) method is called. This method spawns a new process that re-executes 'execute_manager()', and this causes the app to be imported twice (since the second time the app is imported is from a different thread, so the import is really executed twice).

I'm not sure how this should be fix, so I'll mark this as 'design decision needed'.

comment:5 by Natalia Bidart, 14 years ago

Component: UncategorizedCore framework
Keywords: runserver added
Summary: import inside project urls.py import application module twiceApplication module imported twice when running development server
Triage Stage: UnreviewedDesign decision needed

comment:6 by Natalia Bidart, 14 years ago

Owner: changed from nobody to nodoby
Status: assignednew

comment:7 by Natalia Bidart, 14 years ago

Owner: changed from nodoby to nobody

comment:8 by Karen Tracey, 14 years ago

Resolution: invalid
Status: newclosed

Loading an app's __init__.py more than once is not a bug. An app should not be coded to expect that its __init__.py will run once and only once. If you have code the must run only once (guessing here since the original report does not actually describe what problem resulted from this behavior) then you need to ensure that happens by some other method than simply putting it in __init__.py and expecting that it will only get loaded once.

comment:9 by ssadler@…, 13 years ago

Easy pickings: unset
Resolution: invalid
Severity: Normal
Status: closedreopened
Type: Uncategorized
UI/UX: unset

I disagree with the decision to close this ticket. I'm trying to override the admin site, by creating an instance at module level, the same way as is done in django.contrib.admin. It's not working because the module is on the INSTALLED_APPS list as as such gets initialized more than once, causing it to have inconsistent state. I think that the repeated instantiation goes against default Python semantics and also the principle of least surprise, so I'll re-open it for consideration.

comment:10 by anonymous, 13 years ago

Resolution: invalid
Status: reopenedclosed

comment:11 by Marcin Nowak, 8 years ago

Yes, this is a bug and still exists for 1.8.6. For example - the loggers are initialized twice.

Note: See TracTickets for help on using tickets.
Back to Top