[Web-SIG] Global Variables

Titus Brown titus at caltech.edu
Sat Sep 10 18:45:34 CEST 2005


-> 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


More information about the Web-SIG mailing list