Class count1: def get (Self): S. session. count + = 1 return STR (S. session. count) Class count2: def get (Self): S. session. count + = 1 yield STR (S. session. count)
With above two handlers differs only in the way returning information (the 2nd one being a generator), Web. py session actually works with the first one but not the second case.
Simple scenario is:
Web. PY uses "processor", similar to chain-of-responsibility in gof patterns, to add processing steps to the handler functions. session, implemented this way, saves session information automatically when the handler is called and finished:
# Session. PY l83 def _ processor (self, Handler): "" application processor to setup session for every request "self. _ cleanup () self. _ load () Try: Return handler () Finally: Self. _ save ()
In usual case (or: count1), then handler () callthe whole handler function and return the string as the result, which is fine. and the session object has all update in-place then _ save () ed in the Finally block.
In the generator (or, count2) case though, the handler () Only returns a generator object of count2.get (bound to some object) but the handler code is not actually started. therefore, when self. _ save () is being called, it's actually before the session counter
Update. web. PY then uses itertools. chain to unroll the generator and get all output to the client, after the handle_with_processors () call in application. PY line 279, which is all too late already.
I believe that the correct way is actually unroll the generator before returning from the handler () call (application. PY line 239, "Return self. handle ()"). but this hasn't been verified and I need insights onto this.
Link to issue on GitHub: https://github.com/webpy/webpy/issues/235