[PEAK] ConfigurationRoot thread safe?

Phillip J. Eby pje at telecommunity.com
Tue May 10 17:08:36 EDT 2005


At 04:53 PM 5/10/2005 -0400, Erik Rose wrote:
>I have a PEAK-based (though not peak.web-using) web app running inside an 
>MS ASP context. Can I make one ConfigurationRoot instance and use it as 
>the parent for many child components, all running concurrently and living 
>in different threads?

No; the only parts of PEAK that are threadsafe (or at least attempt to be) 
are  all identified as such, and they are a miniscule fraction of PEAK overall.

What you want to do is create a pool of configuration roots (a stack, 
really) and use one per thread.  To get a root for use in a thread, you'd 
use something like:

     pool = []   # initial setup

     def withRoot(func):
         try:
             root = pool.pop()
         except IndexError:
             root = config.makeRoot(...)
         try:
             func(root)
         finally:
             pool.append(root)

So, you'd call 'withRoot(some_func)' to call some_func with a configuration 
root.  In practice, actually, it's more likely that you'd be creating some 
specific component with a new configuration root, and calling the function 
with that component, but the above should give you the general idea, and 
it's threadsafe.


>Hoping to stop parsing .ini files over and over again,

By pushing and popping from the end of the pool, this ensures that 
most-recently-used components are reused, so you will only parse .ini files 
and reinitialize components when the number of threads reaches a new peak 
-- no pun intended.




More information about the PEAK mailing list