logo       

Re: Is Cheetah thread safe?: msg#00023

python.cheetah

Subject: Re: Is Cheetah thread safe?

On Mon, Oct 06, 2003 at 01:09:56PM +0200, JZ wrote:
> Saturday, October 4, 2003, 11:34:56 PM, you wrote:
>
> > Template instances are not thread safe. Each thread should
> > instantiate its own template.
>
> Maybe I should create a singleton class to solve it?
>
> I do not understand. I was using permanent cache for Chetah (without
> father refresh at all).

If you need to cache values across multiple instances, the best place
is in a module global. That's Python's equivalent of a singleton class.
You can't put the module global in the template module itself, but you
can put it in the module of an #extends class, and then have a
$placeholder in that class that accesses it.

Because modules are imported once the first time they are needed, the
shared object will remain accessible through the lifetime of the Webware
process.

If the cache can be fully populated before the threads are started, this
is the easiest way. Just have code in the module (outside the #extends)
class that does it.

But if the cache cannot be populated until a web request is active, you'll
have to do it that way. Since conflicting threads may be running at that
point, you'll have to use a lock (mutex) to prevent the simultaneous
updating of the cache, or to prevent a read in the middle of an update.
Something like this:

===== Superclass.py
import thread
from Cheetah.Template import Template
_cache = {}
_cache_lock = thread.allocate_lock()

class Superclass(Template):
def update_cache(self, key, ARGS):
_cache_lock.acquire() # Waits for lock to be free.
value = EXPENSIVE_CALCULATION(ARGS)
_cache[key] = value
_cache_lock.release()

def read_cache(self, key):
_cache_lock.acquire() # Waits for lock to be free.
value = _cache[key]
_cache_lock.release()
return value
# XXX But what if the key is not in the cache?

===== MyTemplate.tmpl
#extends Superclass
#silent $update_cache(KEY, ARGS)
$read_cache(KEY)

There may be a better lock object among those in 'thread' and 'threading'.

If you *do* want cache values to expire someday, it would be easy enough
to do that. Each $read_cache call could check if it's expired and
refresh it, or you could have a separate thread that periodically
refreshes cache items.

> I do not know how Webware cache works and how
> to interpret its "CacheServletClasses" and "CacheServletInstances"
> variables (from Application.config file)

I think CacheServletInstances keeps a pool of finished instances and
recycles them for future requests; that's the compensation for
instances not being reentrant (thread safe).

Webware does not do output caching a la PHP's ob_start(), which I used
in one application. I wrote some code that cached the output by URL
(including query string). That didn't cache the entire servlet, but
it did avoid doing expensive SQL queries redundantly, which was what
we needed. Some (including me) think Webware *should* offer the
ability to cache the entire servlet output by URI/query string, but
I don't think it's been implemented. Of course, only certain servlets
would want it, not necessarily site-wide.

> You know, I have much bigger experience with PHP and it's Smarty
> templates. Their works in clear and easy to understand way.

If it works for you, great. Cheetah exists because of multiple problems
with PHP, which are problems for us although maybe not for you. If
Smarty existed when Cheetah was created, we didn't know about it, or
at least I didn't. I only knew about PHPLib's Template class.

My feeling about Smarty is it has several features that would be
worthwhile incorportating into Cheetah, other features that we have a
more Pythonic way to do, and other features that are too procedural
(C/Perl/PHP-like) for me to like. An extended analysis is in the
Cheetah Users' Guide, appendix C.6 (Cheetah vs. PHP's Smarty templates).

--
-Mike Orr (aka. Sluggo), mso@xxxxxx (iron@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)
http://iron.cx/ English * Esperanto * Russkiy * Deutsch * Espan~ol


-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise