Using glovar vars

3 views
Skip to first unread message

Steve Patrick

unread,
Aug 14, 2009, 7:34:00 AM8/14/09
to Django users
Hi everybody,
I´m new in Django and maybe my problem is a bit stupid...I would like
to use a global variable (for all the application) to store some info
that I get in my firts page. I know I can send variables using
render_to_response, but is there any other easy way??is it possible to
define a global variable, initialize it in my first page and just use
it??
Thanks for your help

Jani Tiainen

unread,
Aug 14, 2009, 8:13:31 AM8/14/09
to django...@googlegroups.com
Steve Patrick kirjoitti:

> Hi everybody,
> I´m new in Django and maybe my problem is a bit stupid...I would like
> to use a global variable (for all the application) to store some info

No you don't want to. And even it's possible in theory, you shouldn't
even consider that option.

> that I get in my firts page. I know I can send variables using
> render_to_response, but is there any other easy way??is it possible to
> define a global variable, initialize it in my first page and just use
> it??

Store value in session?

First at all, your variable would only live time of request (meaning
from the moment URL is parsed and reponse is sent to your browser). What
happens to your "global" after that? It wouldn't exist on next request.

--
Jani Tiainen

Graham Dumpleton

unread,
Aug 15, 2009, 12:48:34 AM8/15/09
to Django users
That is not true.

Global variables at Python module scope will last for the life time of
the process.

That said, you must still be careful about using them because server
may be multiprocess and/or multithreaded, both of which causes issues
with doing it that way.

Have a read of:

http://code.google.com/p/modwsgi/wiki/ProcessesAndThreading

as it says a bit about that.

Graham

Jani Tiainen

unread,
Aug 15, 2009, 11:11:01 AM8/15/09
to django...@googlegroups.com
Graham Dumpleton kirjoitti:

>
>
> On Aug 14, 10:13 pm, Jani Tiainen <rede...@gmail.com> wrote:
>> Steve Patrick kirjoitti:
>>
>>> Hi everybody,
>>> I´m new in Django and maybe my problem is a bit stupid...I would like
>>> to use a global variable (for all the application) to store some info
>> No you don't want to. And even it's possible in theory, you shouldn't
>> even consider that option.
>>
>>> that I get in my firts page. I know I can send variables using
>>> render_to_response, but is there any other easy way??is it possible to
>>> define a global variable, initialize it in my first page and just use
>>> it??
>> Store value in session?
>>
>> First at all, your variable would only live time of request (meaning
>> from the moment URL is parsed and reponse is sent to your browser). What
>> happens to your "global" after that? It wouldn't exist on next request.
>
> That is not true.

Yes, I lied. But that request lifecycle is only (safe) assumption you
can make.

> Global variables at Python module scope will last for the life time of
> the process.

Yes, but who will guarantee that process will live forever? Or in latter
case:

> That said, you must still be careful about using them because server
> may be multiprocess and/or multithreaded, both of which causes issues
> with doing it that way.

What happens if different requests are passed to totally different
processes - then that global var might not be the same.

Of course if environment where processes, threads and lifecycle is very
controlled and project is kind of one time fire-and-forget it would work.

If that variable is so important, memcached and standard django cache
API can help there also pretty well. And it's safer.

Like I've in my current project one configuration table in database - I
don't want to read it from DB everytime I access something, so I put it
very conveniently in shared memcached. There all Django instances that
have my same app can access it. Very fast, very convenient. And I really
don't have to figure out any details of globals and threading and/or
multiprocess problems

--
Jani Tiainen

Graham Dumpleton

unread,
Aug 16, 2009, 12:27:32 AM8/16/09
to Django users
Did you actually go read the document I referenced and the set of
guidelines it has at the end?

Graham

Steve Patrick

unread,
Aug 17, 2009, 3:28:15 AM8/17/09
to Django users
I`ve got an application developed for PCs and I want to make it
available for mobile devices. There are only some slight differences
between booth two versions, so I think is not worth while to develope
different applications. Instead, I've decided to develope an only
application in which I need a "global" variable that helps me to
remember if the user is using a mobile device or a PC (I know it on my
firts page, looking at the user agent). How can I store that
variable?? Of course, if different users connect to the application
they would have different user agents and thus, different values for
that "global" variable I want to use.
Thanks for your time

David Zhou

unread,
Aug 17, 2009, 3:34:06 AM8/17/09
to django...@googlegroups.com
For what you describe, you're not looking for "global" variables in
the sense that you want some value persisted in the same process.
You're looking at session variables, which are variables persisted for
a specific user session.

See:

http://docs.djangoproject.com/en/dev/topics/http/sessions/

Honestly though, the easiest way that I can think of is just to use
separate URLs. That way, a user could easily use the PC version if he
desires.

-- dz
Reply all
Reply to author
Forward
0 new messages