Comment out the 'loadtest' backend in the 'counter' backend sample so that it does...
[gae-samples.git] / sharded-counters / main.py
blobfcc2eea1d68bd61d912ebac8476099c1914cbffc
1 #!/usr/bin/env python
3 # Copyright 2008 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 """A simple application that demonstrates sharding counters
19 to achieve higher throughput.
21 Demonstrates:
22 * Sharding - Sharding a counter into N random pieces
23 * Memcache - Using memcache to cache the total counter value in generalcounter.
24 """
26 import os
27 import wsgiref.handlers
28 from google.appengine.ext import webapp
29 from google.appengine.ext.webapp import template
30 import generalcounter
31 import simplecounter
33 class CounterHandler(webapp.RequestHandler):
34 """Handles displaying the values of the counters
35 and requests to increment either counter.
36 """
38 def get(self):
39 template_values = {
40 'simpletotal': simplecounter.get_count(),
41 'generaltotal': generalcounter.get_count('FOO')
43 template_file = os.path.join(os.path.dirname(__file__), 'counter.html')
44 self.response.out.write(template.render(template_file, template_values))
46 def post(self):
47 counter = self.request.get('counter')
48 if counter == 'simple':
49 simplecounter.increment()
50 else:
51 generalcounter.increment('FOO')
52 self.redirect("/")
55 def main():
56 application = webapp.WSGIApplication(
58 ('/', CounterHandler),
59 ], debug=True)
60 wsgiref.handlers.CGIHandler().run(application)
63 if __name__ == '__main__':
64 main()