On Mon, 2002-12-02 at 12:07, Michael Engelhart wrote:
> Hi all -
> 
> I wrote about this a month or two ago and didn't really get a response 
> but it's starting to bite me again.
> I'm trying to store LDAP connection objects in the user session and 
> because the underlying LDAP objects (from the python-ldap.sf.net 
> modules) aren't "pickleable", on shutdown of the AppServer, an error is 
> thrown and on top of that the AppServer I guess writes the Session to 
> disk anyway (which it probably shouldn't) and then when I try and 
> restart the AppServer, it exits with the following error:
>    File "./WebKit/SessionMemoryStore.py", line 23, in __init__
>    File "./WebKit/SessionFileStore.py", line 55, in __getitem__
> KeyError: 20021202101928-72a11220b9961fe879564b63447a8d6a

Yes, all types of session stores are persistent -- the only
configuration is when they store stuff to disk (immediately, lazily, or
on AppServer shutdown).  This configuration is really for performance
tweaking, it does not introduce any real change in the behavior of the
session.  I think this is good.  

> My problem that I don't get is why when I tell the AppServer to use a 
> MemoryStore model, it still writes to disk.  I don't want my sessions 
> to persist after their timeout.   There are a million reasons not to 
> want this persistence one of which is that if your objects aren't 
> pickleable, it crashes.
> 
> I think this should be addressed in the next release as I find it hard 
> to understand the reason behind wanting to store ALL sessions to disk 
> on shutdown or timeout.

I think Webware encourages a more robust application through this
behavior, but I can also appreciate that this seems somewhat
paternalistic -- it's your app, after all.

But I really don't think it should be so hard.  You could use some sort
of pool, paramaterized on the username/password.  

If your LDAP access is threadsafe, you could use:
  http://colorstudy.com/software/python/ParamFactory.py

Like:

## AppLDAP.py:

from ParamFactory import ParamFactory

def createLDAPConnection(username, password):
    # yada yada

LDAPConnection = ParamFactory(createLDAPConnection)

## SitePage.py:

import AppLDAP

class SitePage(Page):
    # yada yada

    def ldapConnection(self):
        ses = self.session()
        return AppLDAP.LDAPConnection(ses.value('username'), ses.value('password'))

## end SitePage


If your LDAP connections aren't threadsafe, you'd need some sort of pool
that was paramaterized, something like DBPool except slightly more
general. Off the top of my head (hence untested):

class Pool:
    """
    ``creator`` must be callable, and require only positional (not 
    keyword) arguments.  The arguments must be hashable, i.e., 
    non-mutable (e.g., strings, numbers, tuples)
    """
    def __init__(self, creator, threadsafe=0):
        self._creator = creator
        self._pool = {}
        if threadsafe:
            self.__call__ = self._threadsafe__call__
        else:
            self.__call__ = self._unthreadsafe__call__
            self.returnConnection = self._unthreadsafe_returnConnection
            self._lock = threading.Lock()

    def _threadsafe__call__(self, *args):
        try:
            return self._pool[args]
        except KeyError:
            self._pool[args] = self._creator(*args)
            return self._pool[args]

    def _unthreadsafe__call__(self, *args):
        self._lock.acquire()
        try:
            if not self._pool.has_key(args):
                self._pool[args] = []
            if not self._pool[args]:
                self._pool[args].append(PooledObject(self._unthreadsafe_returnObject, 
                                                     args, self._creator(*args))
            return self._pool[args].pop()
        finally:
            self._lock.release()

    def _unthreadsafe_returnObject(self, args, obj):
        self._pool[args].append(obj)

class PooledObject:
    def __init__(self, callback, args, obj):
        self._callback, self._args = callback, args
        self._obj = obj
    def close(self):
        if self._pool and self._obj is not None:
            self._callback(args, self._obj)
            self._obj = None
    def __del__(self):
        self.close()

## End


There are a multitude of improvements that could be made, but this
should be enough for most pooling needs.  You can use it in your
SitePage similarly to ParamFactory.

-- 
Ian Bicking           Colorstudy Web Development
[EMAIL PROTECTED]   http://www.colorstudy.com
PGP: gpg --keyserver pgp.mit.edu --recv-keys 0x9B9E28B7
4869 N Talman Ave, Chicago, IL 60625 / (773) 275-7241



-------------------------------------------------------
This SF.net email is sponsored by: Get the new Palm Tungsten T 
handheld. Power & Color in a compact size! 
http://ads.sourceforge.net/cgi-bin/redirect.pl?palm0002en
_______________________________________________
Webware-discuss mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/webware-discuss

Reply via email to