4.12. Pool
The pool module provides support for server-pooled variables. That is support
for variables whose lifetime begins when declared, and ends when explicitly
deleted or when the server dies. These variables are often useful for storing
persistent database connections and other information that may be expensive to
compute at each request. Another interesting use of pool variables is to store
file- or memory-based lock objects for concurrency control. A pooled variable
can hold any Python value.
The pool module may be accessed as a regular dictionary, supporting the usual
get, set, delete, has_key, keys, values and clear operations. Note that the pool is shared across all
Spyce files. If file-specific variables are desired, simply include the
filename in the pool variables name as a tuple [i.e. (filename, variable)], or
in some other form.
The pool module also provides access to any server variables that are set in
the Spyce engine configuration
file. A hashtable of these variables is available as pool.server.
The example below shows how the module is used:
examples/pool.spy
|
[[.import names="pool"]]
<html><body>
The pool module supports long-lived server-pooled objects,<br>
useful for database connections, and other variables<br>
that are expensive to compute.<br>
[[\
if pool.has_key('foo'):
print 'Pooled object foo EXISTS.'
else:
pool['foo'] = 1
print 'Pooled object foo CREATED.'
]]
<br>
Value: [[=pool['foo'] ]] <p>
The pool module also gives access to server variables set in
the server configuration file: <br>
[[=pool.server]]<br>
<b>Note:</b> This example requires a long-lived server to
function correctly, i.e. non-CGI environment.
</body></html>
|
Run this code.
(requires Spyce-enabled web server)
|