Python 2.7 has reached end of support
and will be
deprecated
on January 31, 2026. After deprecation, you won't be able to deploy Python 2.7
applications, even if your organization previously used an organization policy to
re-enable deployments of legacy runtimes. Your existing Python
2.7 applications will continue to run and receive traffic after their
deprecation date. We recommend that
you
migrate to the latest supported version of Python.
Stay organized with collections
Save and categorize content based on your preferences.
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.
Parametersevent – 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).
ReturnsA 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.
ReturnsTrue if something happened; False if all queues are empty.
- run_idle()source
Run one of the idle callbacks.
ReturnsTrue 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
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-06-16 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-06-16 UTC."],[[["\u003cp\u003eThe \u003ccode\u003eEventLoop\u003c/code\u003e class manages asynchronous operations, including App Engine RPCs (like urlfetch, memcache, and datastore) and scheduled callback functions, with optional time delays.\u003c/p\u003e\n"],["\u003cp\u003eIdle callbacks can be added, and their return values (None, False, or True) determine if they are removed, rescheduled later, or rescheduled soon, respectively.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eEventLoop\u003c/code\u003e can queue function calls to be executed at a specified time in the future using the queue_call function.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eEventLoop\u003c/code\u003e can manage RPC calls, associating each call with a callback function that is executed upon the RPC's completion.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eget_event_loop\u003c/code\u003e function provides an \u003ccode\u003eEventLoop\u003c/code\u003e instance, with a new instance created per HTTP request or thread.\u003c/p\u003e\n"]]],[],null,["# google.appengine.ext.ndb.eventloop module\n=========================================\n\nSummary\n-------\n\nAn event loop.\n\nThis event loop should handle both asynchronous App Engine RPC objects\n(specifically urlfetch, memcache and datastore RPC objects) and arbitrary\ncallback functions with an optional time delay.\n\nNormally, event loops are singleton objects, though there is no\nenforcement of this requirement.\n\nThe API here is inspired by Monocle.\n\nContents\n--------\n\n*class* google.appengine.ext.ndb.eventloop.EventLoop(clock=None)[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/ndb/eventloop#EventLoop)\n\n: Bases: object\n\n An event loop. \n\n add_idle(callback, \\*args, \\*\\*kwds)[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/ndb/eventloop#EventLoop.add_idle)\n\n : Add an idle callback.\n\n An idle callback can return True, False or None. These mean:\n\n - None: remove the callback (don't reschedule)\n\n - False: the callback did no work; reschedule later\n\n - True: the callback did some work; reschedule soon\n\n If the callback raises an exception, the traceback is logged and\n the callback is removed. \n\n clear()[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/ndb/eventloop#EventLoop.clear)\n\n : Remove all pending events without running any.\n\n insort_event_right(event, lo=0, hi=None)[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/ndb/eventloop#EventLoop.insort_event_right)\n\n : Insert event in queue, and keep it sorted assuming queue is sorted.\n\n If event is already in queue, insert it to the right of the rightmost\n event (to keep FIFO order).\n\n Optional args lo (default 0) and hi (default len(a)) bound the\n slice of a to be searched.\n Parameters\n\n event -- a (time in sec since unix epoch, callback, args, kwds) tuple. \n\n queue_call(delay, callback, \\*args, \\*\\*kwds)[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/ndb/eventloop#EventLoop.queue_call)\n\n : Schedule a function call at a specific time in the future.\n\n queue_rpc(rpc, callback=None, \\*args, \\*\\*kwds)[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/ndb/eventloop#EventLoop.queue_rpc)\n\n : Schedule an RPC with an optional callback.\n\n The caller must have previously sent the call to the service.\n The optional callback is called with the remaining arguments.\n\n NOTE: If the rpc is a MultiRpc, the callback will be called once\n for each sub-RPC. TODO: Is this a good idea? \n\n run()[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/ndb/eventloop#EventLoop.run)\n\n : Run until there's nothing left to do.\n\n run0()[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/ndb/eventloop#EventLoop.run0)\n\n : Run one item (a callback or an RPC wait_any).\n\n Returns\n\n A time to sleep if something happened (may be 0);\n None if all queues are empty. \n\n run1()[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/ndb/eventloop#EventLoop.run1)\n\n : Run one item (a callback or an RPC wait_any) or sleep.\n\n Returns\n\n True if something happened; False if all queues are empty. \n\n run_idle()[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/ndb/eventloop#EventLoop.run_idle)\n\n : Run one of the idle callbacks.\n\n Returns\n\nTrue if one was called, False if no idle callback was called. \n\ngoogle.appengine.ext.ndb.eventloop.add_idle(callback, \\*args, \\*\\*kwds)[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/ndb/eventloop#add_idle)\n: \n\ngoogle.appengine.ext.ndb.eventloop.queue_call(\\*args, \\*\\*kwds)[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/ndb/eventloop#queue_call)\n: \n\ngoogle.appengine.ext.ndb.eventloop.queue_rpc(rpc, callback=None, \\*args, \\*\\*kwds)[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/ndb/eventloop#queue_rpc)\n: \n\ngoogle.appengine.ext.ndb.eventloop.get_event_loop()[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/ndb/eventloop#get_event_loop)\n\n: Return a EventLoop instance.\n\n A new instance is created for each new HTTP request. We determine\n that we're in a new request by inspecting os.environ, which is reset\nat the start of each request. Also, each thread gets its own loop. \n\ngoogle.appengine.ext.ndb.eventloop.run()[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/ndb/eventloop#run)\n: \n\ngoogle.appengine.ext.ndb.eventloop.run0()[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/ndb/eventloop#run0)\n: \n\ngoogle.appengine.ext.ndb.eventloop.run1()[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/ndb/eventloop#run1)\n:"]]