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

--~--~---------~--~----~------------~-------~--~----~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to