While I am laying the foundation of a new client library for working with Datastore with asynchronous query support, I added some low-level functionality that you can use today. The only API with documented support for asynchronous requests is the
urlfetch . And this functionality can be quite useful.
Suppose we need to select certain data from a remote service. The service has 2 instances, both of which are slightly loaded. What we want to do is send requests to both services at the same time (this is the easy part), and then wait for the result from one of them.
from google.appengine.api import urlfetch, apiproxy_stub_map urls = ['http://service1.com', 'http://service2.com'] # Etc. rpcs = [] for url in urls: rpc = urlfetch.create_rpc(deadline=1.0) urlfetch.make_fetch_call(rpc, url) rpcs.append(rpc) rpc = apiproxy_stub_map.UserRPC.wait_any(rpcs) # Now rpc is the first rpc that returned a result. Have at it!
That's all! If you want to learn more about this useful feature, check out the
docstring in the App Engine SDK .
')
You can also call
wait_any () repeatedly to get the next response. Make sure to remove the query that was
processed from the list: the
wait_any () specification tells you that the first completed query is returned from the list, regardless of whether you looked at it or not.
Also keep in mind that at the moment there is no functionality to cancel other requests. The problem is that even if you ignore other requests, App Engine Runtime will still wait for them to be executed or timeout.
Finally, there is another similar
UserRPC.wait_all () method that waits for all requests to complete (it returns nothing).