[Twisted-Python] Re: Reloading of the same `.rpy' file

François Pinard pinard at iro.umontreal.ca
Tue Apr 29 16:10:13 EDT 2003

[Christopher Armstrong]

> On 2003.04.29 10:41, François Pinard wrote:
> > ---------------------------------------------------------------------->
> > import remanent
> > resource = remanent.resource
> > ----------------------------------------------------------------------<
> > 
> > which is a bit ridiculous if it has to be a standard idiom.

> Why is it ridiculous?

Sorry if the word offended anyone, it surely was not my intent to do so.

I only meant that if there is really no advantage at reloading an RPY file,
then there is an advantage at loading it only once (unless such RPY files
are so numerous or so big that it would create a memory problem, but it
might likely not be the case in practice), which is avoiding spurious or
useless re-initialisation.  Then, the proper way would be to use a two-liner
for each such file, and having such small bootstraps is questionable.

> The semantics for .rpy are good because they're flexible; you can either
> recreate your Resource object every time, or you can cache it.  [...]  It
> gives you a choice between CPU usage (recreating your Resource every time)
> and memory usage (keeping your Resources around in memory between
> requests).  [...] Also, mind that .rpys are meant to be as tiny as
> possible; they're just supposed to be glue code between your web app code
> and the web server.

If a resource was consuming a lot of memory, I would understand.  But as you
just explained, RPY files are meant to be tiny, and then unlikely to build
tons of fresh bytes in Python structures.

Yet, within a small RPY file, one might import from library big modules
having Resource based classes, and resources are meant to be freed
immediately after having been consumed.  For example, such a library
resource might define some big triple-quoted static string holding the
template of an HTML page.  But then, the module will stay imported after the
RPY file disappears, the big string will not be freed, and reloading will
not trigger significant memory savings.  If the template string gets read
from disk each time, it will likely be kept in a local variable in the
`render' method, and will be freed as soon as we exit that method, so
reloading does not in itself trigger memory savings in that case either.

So, just to help me understand, could you illustrate by an example, a likely
case demonstrating the flexibility gained by multiple reloading?

> Also, IIRC there's some sort of caching mechanism that glyph implemented for
> rpys, but I don't remember how it works.

It might be those Registry instances which Moshe referred to in a recent
message, and which are sketched a bit quickly in the book, page 23, (2.3.2
"Using Twisted.Web", section "Resource Scripts").

François Pinard   http://www.iro.umontreal.ca/~pinard

More information about the Twisted-Python mailing list