athena.LivePage calculator sample

Werner Thie <[email protected]> Wed, 31 Jan 2007 12:58:39 +0100
Newsgroups gmane.comp.python.quotient.dev
Message-ID <[email protected]>
Hi all

I am quite new to nevow and tried to understand the calculator example.

 From reading the code I expected to have one and only Calculator object 
being created per LivePage rendered, but as the appended session log 
shows, this is clearly not the case.

How can this be achieved?

I am greateful for any enlightening comment!

Thanks, Werner

---- commented session log ------------------------------------------

running svn trunk version of nevow with Python25

- JScript getElement is not available anymore, the browser chokes, had 
to change it to

         updateDisplay = function(expression)
         {
             document.getElementById('output').innerHTML = expression;
         }

- when running all the examples with 'twistd -noy examples.tac' every 
browser I connect with to the calculator resource gets its 'own' 
calculator as expected

- when running the calculator.py as a standalone webserver as suggested 
by the code under __main__ with 'python calculator.py' every browser 
gets the single calculator object which was created with

calculator = Calculator()
site = appserver.NevowSite(calculator)

thus sharing self.expression (which is clearly not what's intended) but 
becomes clear, when looking at

     def calculatorResourceFactory(original):
       return CalculatorResource(ICalculator, original)

calculatorResourceFactory simply returns the same object over and over

- changing calculatorResourceFactory to

     def calculatorResourceFactory(original):
       return CalculatorResource(ICalculator, Calculator())

changes the behavior of the standalone version to be the same as the one 
run with 'twistd -noy examples.tac'

- I was now wondering how many times a Calculator object was created and 
was quite surprised to see 12 objects being created by calls to 
calculatorResourceFactory until the calculator was rendered in the 
browser, although from then on only one calculator object was used for 
callbacks, the eleven other ones hopefully be gc'ed (I didn't 
investigate if references are kept somewhere)

Conceptually I like the calculator sample, because it illustrates the 
LivePage concepts in a surprisingly simple way, but what I expected 
seems not to be congruent with what I observe :-((

How can the 'mass creation' of calculator objects be prevented?