At 11:53 AM 5/23/01 -0500, Ian Bicking wrote:
>J�rn Schrader <[EMAIL PROTECTED]> wrote:
> > I would like to use MiddleKit in a WebKit Application. Can anyone describe
> > the best way to embed a middlekit store in the WebKit Application, so 
> that I
> > dont have to open it for each request.
>
>I have a SitePage class that all pages inherit from.  I include a
>method:
>
>         def store(self):
>                 if not hasattr(self, "_store"):
>                         self._store = MySQLObjectStore(user='username', 
> passwd='password')
> 
>self._store.readModelFileNamed(os.path.join('Gallery'))
>                 return self._store
>
>Then I just call self.store() to get the store.
>
>It might be better to do:
>
>     def store(self):
>         try:
>             return self._store
>         except AttributeError:
>             self._store = [...]
>             return self._store
>
>   Ian

This isn't thread-safe -- 2 servlets could create the store at the same time.

You're better off putting the store initialization into a module that gets 
imported -- then Python automatically ensures that only one thread imports 
it at once.  (Either that or you should explicitly use a threading.Lock 
object to make it thread-safe.)

If you want to "pre-load" the store when the app server starts up to reduce 
response time, then you can put the import into the __init__.py in your 
context directory.  __init__.py automatically gets imported when the app 
server starts.


--

- Geoff Talvola
   [EMAIL PROTECTED]

_______________________________________________
Webware-discuss mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to