[Zope-dev] real globals
Robin Becker
robin@jessikat.demon.co.uk
Sun, 25 Jul 1999 00:35:33 +0100
In article <$GVuiAARObm3EwjH@jessikat.demon.co.uk>, Robin Becker
<robin@jessikat.demon.co.uk> writes
>What is the correct way for an ExtensionMethod to share data across all
>invocations. I have an extension method which does a mem/cpu intensive
>calculation. I have implemented a threaded queuing scheme using a global
>variable _Jobs; this variable is supposed to be unique. When I run the
>extension function twice simultaneously I get two copies of this
>'global' instead of the expected 1; my function proceeds as expected,
>but the memory usage becomes extreme and thrashing starts.
>
>So my question is how to get the same environment for each invocation?
I have implemented a solution to my problem in the following way
1) package everything into a class whose __init__ creates the _Jobs
queue.
2) hook a singleton instance into __main__
3) use the singleton calc method
def __ZGA():
'returns the singleton'
if not hasattr(sys.modules['__main__'],'__ZGA'):
sys.modules['__main__'].__dict__['__ZGA']=ZGA_calculator()
return sys.modules['__main__'].__dict__['__ZGA']
def ZGA_CALC():
return __ZGA()._calc()
--
Robin Becker