Lincoln H. wrote:
> Thanks for all who replied with good answers. I am working on fixing
> this problem.
> Now I am curious about the technical design of the servlet. I wrongfully
> assumed
> that a new servlet is loaded per connection, now I understood it is not
> the case.
> Because the servlet is re-used, that's why whatever I declare as part of
> the class of the
> servlet is shared by different connections. Since the servlet is loaded
> once and shared
> among all connections, what are the exact parts that explicitly tells
> the servlet to keep
> the data unique to the connection - I assume this would be the session
> right? Is it better
> to put the non-shared data into __init__ (which is still part of the
> servlet class) or awake() ?
>
> Lincoln
Hi Lincoln,
__init__() is called at webware startup (when it first loads the
servlet). whatever you put in here persists for the duration of the
servlet's lifetime. the servlet's lifetime could be equal to the webware
server's lifetime (weeks, months, years).
awake() is called when the servlet is woken up for a new request. any
request specific initialization should be done here. this will of course
persist along with the servlet just like __init__, so...
sleep() is called when the servlet is finished handling a request. you
need to make sure to clean up any variables etc. that were set during
awake or during the process of the request. do whatever you need to do
to clean up for the next request.
Recap:
__init__ - set up servlet on app server startup - runs only once
awake() - set up request - runs once per request
sleep() - clean up request - runs once per request
to put this all together, here is a simple example from one of my
SitePage classfiles:
def awake(self, transaction):
Page.awake(self, transaction)
self.script_uri = self.request().serverDictionary()['SCRIPT_URI']
self.success = ''
self.error = ''
self.message = ''
def sleep(self, transaction):
self.mysql.Disconnect()
Page.sleep(self, transaction)
and here is the source code from the webkit Page classfile:
def awake(self, transaction):
HTTPServlet.awake(self, transaction)
self._response = transaction.response()
self._request = transaction.request()
self._session = None # don't create unless needed
assert self._transaction is not None
assert self._response is not None
assert self._request is not None
As you can see, it is important to call the parent's awake method -
Page.awake() - if you override awake. Same goes for sleep.
Hope this helps you to get an idea about the design/flow of webkit. I
find that a little time spent browsing the webkit source code
(especially Page, Session, and other well used classes) really helps my
understanding of how the framework works.
Considering that the python code is well written and fairly simple, and
that to use webkit we actually *extend* it, the source code presents
itself as the best documentation for this project.
- Sam Nilsson
|