Last active
March 7, 2017 04:49
-
-
Save edwardfung123/e106f87e139934e8a0958c12129a2747 to your computer and use it in GitHub Desktop.
GAE Concurrency Test1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import webapp2 | |
| from google.appengine.ext import ndb | |
| class BankAccount(ndb.Model): | |
| amount = ndb.IntegerProperty(default=0) | |
| redirect_html = '''<html> | |
| <head> | |
| <meta http-equiv="refresh" content="1; url=http://{host}/" /> | |
| </head> | |
| <body><pre>Will redirect you back to home page in 1 second.</pre></body> | |
| </html> | |
| ''' | |
| class MainHandler(webapp2.RequestHandler): | |
| def get(self): | |
| key = 'edward' | |
| account = BankAccount.get_by_id(key) | |
| if not account: | |
| self.abort(404, 'Your account not found. may be create one?') | |
| self.response.headers['content-type'] = 'text/plain' | |
| self.response.write('Hello\n{} you have ${} in your account.'.format(key, account.amount)) | |
| def init(self): | |
| model = BankAccount.get_or_insert('edward', amount=0) | |
| model.amount = 0 | |
| model.put() | |
| self.redirect('/') | |
| def inc(self): | |
| key = 'edward' | |
| account = BankAccount.get_by_id(key) | |
| account.amount += 1 | |
| account.put() | |
| self.response.write(redirect_html.format(host=self.request.host)) | |
| app = webapp2.WSGIApplication([ | |
| webapp2.Route('/init', handler='main.MainHandler:init'), | |
| webapp2.Route('/inc', handler='main.MainHandler:inc'), | |
| ('/*', MainHandler), | |
| ], debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment