Ian Bicking said:
> Sessions can be accessed from multiple threads, though usually that
> would only happen when a browser was downloading dependent files (e.g.,
> images), that were also served off WebKit. But you still don't want to
> put these in the session, because they aren't pickleable, and the
> session persists over restarts by using pickle.
>
> You should either be able to use DBPool (or something like it), or do
> some other simple resource sharing. Lists work better than you might
> think:
>
> _storage =3D []
> def getResource():
> try:
> return _storage.pop()
> except IndexError:
> return makeNewResource()
> def returnResource(rsrc):
> _storage.append(rsrc)
>
> .append and .pop are threadsafe (enough) so it's pretty simple to
> implement.
>
I'm not sure if we have made it clear that regardless of what "pooling"
method you use, the variable that stores the reference to the pool should
be defined at either the module level or class level, not at the instance
level. This insures that only a single instance of the pool exists for
the entire python process.
The above example is of a module level pool referenced by the "_storage"
variable.
Here is an example of the pool reference at the class level:
class SomeClass:
# Do this, since it creates a class variable.
_storage =3D []
def __init__(self, ...):
# Don't do this, since it creates an instance variable.
#self._storage =3D []
def getResource(self):
try:
return self._storage.pop()
except IndexError:
return self.makeNewResource()
def returnResource(self, rsrc):
self._storage.append(rsrc)
The choice of pooling method and where you put the reference to the pool
depends on your application.
--=20
Warren Smith
warren@...
|