Santiago Caracol wrote: >> Python offers an elegant mechanism to calculate values on demand: the >> generator function: >> >> >>> def calculate_answers(): >> >> ... for i in range(100): >> ... print "calculating answer #%d" % i >> ... yield i * i >> ... >> > > Thanks for pointing this out. I was aware of the yield statement. > > My problem is this: > > (1) The user submits a query: > > --------------------------------- > | What is the answer? | > --------------------------------- > <Submit> > > (2) The web server gives the user N answers and a button saying "More > answers": > > . answer 1 > . answer 2 > . answer 3 > > <More answers> > > At this stage the function that writes html to the user has been > called. The call must be terminated, or else, the user doesn't get any > html. This means that the call of the answer-calculating function, > whether it uses yield or not, is also terminated. This means, when the > user presses the <More answers>-button, the calculation has to start > at the beginning.
Adapted from the wsgiref documentation at http://docs.python.org/library/wsgiref.html $ cat wsgi_demo.py from wsgiref.util import setup_testing_defaults from wsgiref.simple_server import make_server from itertools import count, islice def answers(): for i in count(): yield "Answer #%d\n" % i gen = answers() def simple_app(environ, start_response): setup_testing_defaults(environ) status = '200 OK' headers = [('Content-type', 'text/plain')] start_response(status, headers) return islice(gen, 3) httpd = make_server('', 8000, simple_app) print "Serving on port 8000..." httpd.serve_forever() Run the above with $ python wsgi_demo.py Serving on port 8000... Now point your favourite browser to http://localhost:8000/ and hit refresh. Peter -- http://mail.python.org/mailman/listinfo/python-list