App Engine Python SDK  v1.6.9 rev.445
The Python runtime is available as an experimental Preview feature.
Classes | Functions | Variables
google.appengine.ext.ndb.tasklets Namespace Reference

Classes

class  _State
 
class  Future
 
class  MultiFuture
 
class  QueueFuture
 
class  ReducingFuture
 
class  SerialQueueFuture
 

Functions

def add_flow_exception
 
def sleep
 
def get_return_value
 
def tasklet
 
def synctasklet
 
def toplevel
 
def get_context
 
def make_default_context
 
def make_context
 
def set_context
 

Variables

list __all__
 
 _logging_debug = utils.logging_debug
 
tuple _state = _State()
 
tuple _flow_exceptions = ()
 
 Return = StopIteration
 
string _CONTEXT_KEY = '__CONTEXT__'
 

Detailed Description

A tasklet decorator.

Tasklets are a way to write concurrently running functions without
threads; tasklets are executed by an event loop and can suspend
themselves blocking for I/O or some other operation using a yield
statement.  The notion of a blocking operation is abstracted into the
Future class, but a tasklet may also yield an RPC in order to wait for
that RPC to complete.

The @tasklet decorator wraps generator function so that when it is
called, a Future is returned while the generator is executed by the
event loop.  Within the tasklet, any yield of a Future waits for and
returns the Future's result.  For example:

@tasklet
def foo():
  a = yield <some Future>
  b = yield <another Future>
  raise Return(a + b)

def main():
  f = foo()
  x = f.get_result()
  print x

Note that blocking until the Future's result is available using
get_result() is somewhat inefficient (though not vastly -- it is not
busy-waiting).  In most cases such code should be rewritten as a tasklet
instead:

@tasklet
def main_tasklet():
  f = foo()
  x = yield f
  print x

Calling a tasklet automatically schedules it with the event loop:

def main():
  f = main_tasklet()
  eventloop.run()  # Run until no tasklets left to do
  f.done()  # Returns True

As a special feature, if the wrapped function is not a generator
function, its return value is returned via the Future.  This makes the
following two equivalent:

@tasklet
def foo():
  return 42

@tasklet
def foo():
  if False: yield  # The presence of 'yield' makes foo a generator
  raise Return(42)  # Or, after PEP 380, return 42

This feature (inspired by Monocle) is handy in case you are
implementing an interface that expects tasklets but you have no need to
suspend -- there's no need to insert a dummy yield in order to make
the tasklet into a generator.

Function Documentation

def google.appengine.ext.ndb.tasklets.add_flow_exception (   exc)
Add an exception that should not be logged.

The argument must be a subclass of Exception.
def google.appengine.ext.ndb.tasklets.sleep (   dt)
Public function to sleep some time.

Example:
  yield tasklets.sleep(0.5)  # Sleep for half a sec.
def google.appengine.ext.ndb.tasklets.synctasklet (   func)
Decorator to run a function as a tasklet when called.

Use this to wrap a request handler function that will be called by
some web application framework (e.g. a Django view function or a
webapp.RequestHandler.get method).
def google.appengine.ext.ndb.tasklets.toplevel (   func)
A sync tasklet that sets a fresh default Context.

Use this for toplevel view functions such as
webapp.RequestHandler.get() or Django view functions.

Variable Documentation

list google.appengine.ext.ndb.tasklets.__all__
Initial value:
1 = ['Return', 'tasklet', 'synctasklet', 'toplevel', 'sleep',
2  'add_flow_exception', 'get_return_value',
3  'get_context', 'set_context',
4  'make_default_context', 'make_context',
5  'Future', 'MultiFuture', 'QueueFuture', 'SerialQueueFuture',
6  'ReducingFuture',
7  ]