[Zope-dev] thread safety in external methods

Michel Pelletier michel@digicool.com
Sat, 21 Aug 1999 09:41:00 -0400


Robin Becker wrote:
> 
> How do I do thread safety inside an External method.
> 
> I want to access a global singleton as
> 
> def __ZGA():
>     if not hasattr(sys.modules['__main__'],'__ZGA'):
>         #unsafe here...
>         Z=ZGA_calculator()
>         # initialise things to do with Z
>         sys.modules['__main__'].__dict__['__ZGA']=Z
>     return sys.modules['__main__'].__dict__['__ZGA']
> 
> the problem is controlling access to sys.modules['__main__'].__dict__['__ZGA']. I can't have a
> global lock inside my external method as I get a different environment with each call (which is
> why I need the singleton in the first place).
> 
> It would be sufficient for my purposes if I could guarantee to get certain code run just once
> at startup time.
> 

When Zope starts up it imports all the modules containing external
methods.  It might be possible to place code directly in the module for
execution upon import.

I'm not 100% certain, however, but I don't see why this standard python
feature would have been disabled.

-Michel


> Do I have to make a product or is there some easier way to do this.
> --
> Robin Becker
> 
> _______________________________________________
> Zope-Dev maillist  -  Zope-Dev@zope.org
> http://www.zope.org/mailman/listinfo/zope-dev
> 
> (To receive general Zope announcements, see:
> http://www.zope.org/mailman/listinfo/zope-announce
> 
> For non-developer, user-level issues,
> zope@zope.org, http://www.zope.org/mailman/listinfo/zope )