Created
August 21, 2012 00:50
-
-
Save dylanvee/3410076 to your computer and use it in GitHub Desktop.
conditionally async tasklet decorator
This file contains 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
from google.appengine.ext import ndb | |
def tasklet(func): | |
"""Tasklet decorator that lets the caller specify either async or sync | |
behavior at runtime. | |
If make_sync is False (the default), the tasklet returns a future and | |
can be used in asynchronous control flow from within other tasklets | |
(like ndb.tasklet). If make_sync is True, the tasklet will wait for its | |
results and return them, allowing you to call the tasklet from synchronous | |
code (like ndb.synctasklet). | |
""" | |
@ndb.utils.wrapping(func) | |
def tasklet_wrapper(*args, **kwds): | |
arg_name = "make_sync" | |
sync_by_default = False | |
make_sync = kwds.get(arg_name, sync_by_default) | |
if make_sync: | |
taskletfunc = ndb.synctasklet(func) | |
else: | |
taskletfunc = ndb.tasklet(func) | |
if arg_name in kwds: | |
del kwds[arg_name] | |
return taskletfunc(*args, **kwds) | |
return tasklet_wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment