Global Variables

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Global Variables

tsoehnli
Hello all,

        I am working on an ongoing project, and recently merged all the different segments of the request into a single variable.  To propogate that variable,across function calls, and imported modules, I had to pass it as a variable to each function. As is visible, this is a very redundant, and unnecessary action.  I recently discovered that I could imbed the variable into __builtins__, and the variable is accessible throughout the entire request process.  This seems to work so far when the request is in CGI mode and when the request is being handled by mod_python.  I was wondering if anyone out there has dealt with this before, and knows whether or not there is an issue in using __builtins__ as a way of handling global variables, especially in respect to the way mod_python handles the use of variables.  I have no desire for the variable to be overwritten, or redefined in the middle of a request.  

                                                                     
                         Sincerely,
                                                               
                            Timothy Soehnlin
                                                                       

--
I would rather be known as a Christian
        and despised, than to be overlooked,
                and thought of as one of the world.
_______________________________________________
Web-SIG mailing list
[hidden email]
Web SIG: http://www.python.org/sigs/web-sig
Unsubscribe: http://mail.python.org/mailman/options/web-sig/lists%40nabble.com
Reply | Threaded
Open this post in threaded view
|

Re: Global Variables

Robert Brewer
Timothy Soehnlin wrote:

> I am working on an ongoing project, and recently
> merged all the different segments of the request into a
> single variable.  To propogate that variable,across function
> calls, and imported modules, I had to pass it as a variable
> to each function. As is visible, this is a very redundant,
> and unnecessary action.  I recently discovered that I could
> imbed the variable into __builtins__, and the variable is
> accessible throughout the entire request process.  This seems
> to work so far when the request is in CGI mode and when the
> request is being handled by mod_python.  I was wondering if
> anyone out there has dealt with this before, and knows
> whether or not there is an issue in using __builtins__ as a
> way of handling global variables, especially in respect to
> the way mod_python handles the use of variables.  I have no
> desire for the variable to be overwritten, or redefined in
> the middle of a request.  

If you ever make your application multithreaded, that approach is going
to cause problems, as competing threads overwrite your request object's
attributes. You can do what CherryPy did, and use global threadlocal
objects instead. Or you could save yourself the headache and just use
CherryPy to develop your app. ;)


Robert Brewer
System Architect
Amor Ministries
[hidden email]
_______________________________________________
Web-SIG mailing list
[hidden email]
Web SIG: http://www.python.org/sigs/web-sig
Unsubscribe: http://mail.python.org/mailman/options/web-sig/lists%40nabble.com
Reply | Threaded
Open this post in threaded view
|

Re: Global Variables

Shannon -jj Behrens
I second the statement about multi-threading headaches if you use
globals in this way.

In Aquarium, since I have to support different threading API's (no
threads, Python threads, coroutines), I put everything in a Context
object, and then pass that Context object to everything's constructor.
 This isn't as bad as it sounds.  It happens automatically when I call
aquariumFactory(moduleName, *args, **kargs) which does a dynamic
import, instantiates the class passing the Context object and the
additional args, and then returns the instance.

Best Regards,
-jj

On 9/9/05, Robert Brewer <[hidden email]> wrote:

> Timothy Soehnlin wrote:
> > I am working on an ongoing project, and recently
> > merged all the different segments of the request into a
> > single variable.  To propogate that variable,across function
> > calls, and imported modules, I had to pass it as a variable
> > to each function. As is visible, this is a very redundant,
> > and unnecessary action.  I recently discovered that I could
> > imbed the variable into __builtins__, and the variable is
> > accessible throughout the entire request process.  This seems
> > to work so far when the request is in CGI mode and when the
> > request is being handled by mod_python.  I was wondering if
> > anyone out there has dealt with this before, and knows
> > whether or not there is an issue in using __builtins__ as a
> > way of handling global variables, especially in respect to
> > the way mod_python handles the use of variables.  I have no
> > desire for the variable to be overwritten, or redefined in
> > the middle of a request.
>
> If you ever make your application multithreaded, that approach is going
> to cause problems, as competing threads overwrite your request object's
> attributes. You can do what CherryPy did, and use global threadlocal
> objects instead. Or you could save yourself the headache and just use
> CherryPy to develop your app. ;)
>
>
> Robert Brewer
> System Architect
> Amor Ministries
> [hidden email]
> _______________________________________________
> Web-SIG mailing list
> [hidden email]
> Web SIG: http://www.python.org/sigs/web-sig
> Unsubscribe: http://mail.python.org/mailman/options/web-sig/jjinux%40gmail.com
>


--
I have decided to switch to Gmail, but messages to my Yahoo account will
still get through.
_______________________________________________
Web-SIG mailing list
[hidden email]
Web SIG: http://www.python.org/sigs/web-sig
Unsubscribe: http://mail.python.org/mailman/options/web-sig/lists%40nabble.com
Reply | Threaded
Open this post in threaded view
|

Re: Global Variables

Titus Brown
-> I second the statement about multi-threading headaches if you use
-> globals in this way.
->
-> In Aquarium, since I have to support different threading API's (no
-> threads, Python threads, coroutines), I put everything in a Context
-> object, and then pass that Context object to everything's constructor.
->  This isn't as bad as it sounds.  It happens automatically when I call
-> aquariumFactory(moduleName, *args, **kargs) which does a dynamic
-> import, instantiates the class passing the Context object and the
-> additional args, and then returns the instance.

You can also write a function to get the variable,

_global_var = None

def init_global_var():
   global _global_var
   _global_var = GlobalSomethingOrOther()

def get_global_var():
   return _global_var

Then in the case of multithreaded apps, you can change these functions
to use a dictionary of "global variables" indexed by thread-id.

--titus
_______________________________________________
Web-SIG mailing list
[hidden email]
Web SIG: http://www.python.org/sigs/web-sig
Unsubscribe: http://mail.python.org/mailman/options/web-sig/lists%40nabble.com