python - Why is an instance of webapp.WSGIApplication always defined as a global variable in google app engine code? -
i'm starting learn use google app engine and, in of code i've come across, declare instance of webapp.wsgiapplication global variable. doesn't seem necessary, code works fine when locally declared in main function. advised global variables should avoided. there good, or not good, reason it's done way?
example:
class guestbook(webapp.requesthandler): def post(self): greeting = greeting() if users.get_current_user(): greeting.author = users.get_current_user() greeting.content = self.request.get('content') greeting.put() self.redirect('/') application = webapp.wsgiapplication([ ('/', mainpage), ('/sign', guestbook)], debug=true) def main(): wsgiref.handlers.cgihandler().run(application)
why not following, works:
class guestbook(webapp.requesthandler): def post(self): greeting = greeting() if users.get_current_user(): greeting.author = users.get_current_user() greeting.content = self.request.get('content') greeting.put() self.redirect('/') def main(): application = webapp.wsgiapplication([ ('/', mainpage), ('/sign', guestbook)], debug=true) wsgiref.handlers.cgihandler().run(application)
this works in examples multiple request handlers.
google app engine offers neat feature called app caching.
first time main handler invoked, full script evaluated importing modules , creating global elements.
if handler called after script has been evaluated, app instance calls main()
function directly.
the overhead of creating global elements payed first time , objects created can reused multiple requests saving time , resources.
that said, highly recommended pick first choice, declaring application
variable outside main()
function.
Comments
Post a Comment