Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
persistence of variable in views.py
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
skunkwerk  
View profile  
 More options May 1 2008, 2:23 am
From: skunkwerk <skunkw...@gmail.com>
Date: Wed, 30 Apr 2008 23:23:42 -0700 (PDT)
Local: Thurs, May 1 2008 2:23 am
Subject: persistence of variable in views.py
I have a variable defined by a function call in my views.py that's at
the global level at the start of the file (ie is not inside any other
function, though the variable is not prefixed by 'global').  As this
takes a few seconds to complete, I just wanted to make sure that this
function isn't being called every time a new url is requested.  django
only loads views.py once and then calls whatever functions necessary,
correct?

thanks


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Bennett  
View profile  
 More options May 1 2008, 3:18 am
From: "James Bennett" <ubernost...@gmail.com>
Date: Thu, 1 May 2008 02:18:07 -0500
Local: Thurs, May 1 2008 3:18 am
Subject: Re: persistence of variable in views.py

On Thu, May 1, 2008 at 1:23 AM, skunkwerk <skunkw...@gmail.com> wrote:
>  I have a variable defined by a function call in my views.py that's at
>  the global level at the start of the file (ie is not inside any other
>  function, though the variable is not prefixed by 'global').  As this
>  takes a few seconds to complete, I just wanted to make sure that this
>  function isn't being called every time a new url is requested.  django
>  only loads views.py once and then calls whatever functions necessary,
>  correct?

Yes and no.

In theory, under a production setup, the module containing your views
will be initialized only once per server process, and will remain in
memory for the life of the server process (so, for example, if you
have one server process with MaxRequestsPerChild set to "100", it
would be initialized once per 100 HTTP requests).

In practice, it may be initialized multiple times depending on how
you've written your code, specifically depending on the import
statements being used.

When you type, for example::

    from myapp.views import some_view

for the first time, Python will locate the necessary file and
initialize it by executing the code within; this will define the view
functions, and also define your global variable. Then the resulting
object (a live Python module object) will be inserted into the
dictionary "sys.modules" under the key "myapp.views". Later, if you
import again, Python will find "myapp.views" as a key in "sys.modules"
and use the already-initialized module object there.

The problem is that if you later do, say::

    from myproject.myapp.views import some_view

then Python will be looking in "sys.modules" for the key
"myproject.myapp.views", which won't exist; it will go out and find
the file again, initialize it again, and store the resulting module
object in "sys.modules" under the key "myproject.myapp.views".

The net result is that Python will initialize the module once for each
different "path" (e.g., "myapp.views", "myproject.myapp.views", etc.)
you use in an import statement. This is, incidentally, what trips up
so many people with newforms-admin at the moment -- when they
reference the same module in a different way, it gets initialized
again and triggers an error as a model tries to re-register its admin
interface.

To avoid this, ensure that you use import statements consistently.

--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »