google.appengine.ext.ndb.eventloop module

Summary

An event loop.

This event loop should handle both asynchronous App Engine RPC objects (specifically urlfetch, memcache and datastore RPC objects) and arbitrary callback functions with an optional time delay.

Normally, event loops are singleton objects, though there is no enforcement of this requirement.

The API here is inspired by Monocle.

Contents

class google.appengine.ext.ndb.eventloop.EventLoop(clock=None)source

Bases: object

An event loop.

add_idle(callback, *args, **kwds)source

Add an idle callback.

An idle callback can return True, False or None. These mean:

  • None: remove the callback (don’t reschedule)

  • False: the callback did no work; reschedule later

  • True: the callback did some work; reschedule soon

If the callback raises an exception, the traceback is logged and the callback is removed.

clear()source

Remove all pending events without running any.

insort_event_right(event, lo=0, hi=None)source

Insert event in queue, and keep it sorted assuming queue is sorted.

If event is already in queue, insert it to the right of the rightmost event (to keep FIFO order).

Optional args lo (default 0) and hi (default len(a)) bound the slice of a to be searched.

Parameters

event – a (time in sec since unix epoch, callback, args, kwds) tuple.

queue_call(delay, callback, *args, **kwds)source

Schedule a function call at a specific time in the future.

queue_rpc(rpc, callback=None, *args, **kwds)source

Schedule an RPC with an optional callback.

The caller must have previously sent the call to the service. The optional callback is called with the remaining arguments.

NOTE: If the rpc is a MultiRpc, the callback will be called once for each sub-RPC. TODO: Is this a good idea?

run()source

Run until there’s nothing left to do.

run0()source

Run one item (a callback or an RPC wait_any).

Returns

A time to sleep if something happened (may be 0); None if all queues are empty.

run1()source

Run one item (a callback or an RPC wait_any) or sleep.

Returns

True if something happened; False if all queues are empty.

run_idle()source

Run one of the idle callbacks.

Returns

True if one was called, False if no idle callback was called.

google.appengine.ext.ndb.eventloop.add_idle(callback, *args, **kwds)source
google.appengine.ext.ndb.eventloop.queue_call(*args, **kwds)source
google.appengine.ext.ndb.eventloop.queue_rpc(rpc, callback=None, *args, **kwds)source
google.appengine.ext.ndb.eventloop.get_event_loop()source

Return a EventLoop instance.

A new instance is created for each new HTTP request. We determine that we’re in a new request by inspecting os.environ, which is reset at the start of each request. Also, each thread gets its own loop.

google.appengine.ext.ndb.eventloop.run()source
google.appengine.ext.ndb.eventloop.run0()source
google.appengine.ext.ndb.eventloop.run1()source