Skip to content

Commit

Permalink
Deferred example
Browse files Browse the repository at this point in the history
  • Loading branch information
tianhuil committed Apr 24, 2015
1 parent c816aef commit e32da1b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
3 changes: 3 additions & 0 deletions app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ handlers:
- url: .* # This regex directs all routes to main.app
script: main.app

builtins:
- deferred: on

# Third party libraries that are included in the App Engine SDK must be listed
# here if you want to use them. See
# https://developers.google.com/appengine/docs/python/tools/libraries27 for
Expand Down
33 changes: 27 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,41 @@
# Note: We don't need to call run() since our application is embedded within
# the App Engine WSGI application server.

from google.appengine.ext import ndb
from google.appengine.ext import deferred

class Counter(ndb.Model):
count = ndb.IntegerProperty(indexed=False)

def reset():
ndb.delete_multi(Counter().query().fetch(keys_only=True, use_cache=False, use_memcache=False))

def increment():
counter = Counter().query().get(use_cache=False, use_memcache=False)
if not counter:
counter = Counter(count=0)

counter.count += 1
counter.put()

if counter.count < 10:
deferred.defer(increment)

@app.route('/')
def hello():
"""Return a friendly HTTP greeting."""
return 'Hello World!'
"""Return a friendly HTTP greeting."""
reset()
deferred.defer(increment)
return 'Hello World!'


@app.errorhandler(404)
def page_not_found(e):
"""Return a custom 404 error."""
return 'Sorry, Nothing at this URL.', 404
"""Return a custom 404 error."""
return 'Sorry, Nothing at this URL.', 404


@app.errorhandler(500)
def application_error(e):
"""Return a custom 500 error."""
return 'Sorry, unexpected error: {}'.format(e), 500
"""Return a custom 500 error."""
return 'Sorry, unexpected error: {}'.format(e), 500

0 comments on commit e32da1b

Please sign in to comment.