Asynchronous URL Fetch manager for App Engine
App Engine’s URL Fetch API supports fetching URLs asynchronously. However, a request handler may only simultaneously fetch up to 10 URLs. To fetch more than 10, it must wait for one to finish before starting another. This is a little tricky to do efficiently*, so I put together a Python module which takes care of the details. The module provides an AsyncURLFetchManager class with a simple interface – just tell it what URLs you want and it fetches them as quickly as possible. This interface also simplifies the starting of an asynchronous request into a single method call:
fetch_asynchronously(url)
You can also pass fetch_asynchronously() any arguments which urlfetch.make_fetch_call() accepts (e.g., method, payload). You can also ask it for a callback which will conveniently include the RPC object (which contains the results) as well as any other positional or keyword arguments you would like.
At the end of your request, just call wait() to ensures that any pending fetches and their callbacks are completed prior to the request handler terminating.
* Unfortunately, App Engine does not currently provide select() or any other non-blocking mechanism which can check if an RPC has completed. Once it does, this implementation could be improved to ensure that it only waits on an RPC which has already completed (currently we just have to wait on the oldest one – this is sub-optimal since later RPCs may actually finish first).