O'Reilly logo

Programming Google App Engine by Dan Sanderson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Asynchronous Requests in Python

All of the calls to the URL Fetch service we’ve seen so far in this chapter have been synchronous calls: the app asks the service to fetch a URL, then waits for the service to return the result. But time is precious within a web request, and waiting for a remote host to return data is likely to be the most time-consuming part of a request handler. If the handler must fetch more than one URL, these wait times add up as the app must wait for the first host to respond before starting the next fetch. But while the service is busy fetching URLs, the app server is doing nothing. The app could be using this valuable clock time to perform other tasks.

To support this, the URL Fetch service supports asychronous calls. With an asynchronous call, the app starts a URL fetch by calling the service with the request data as usual. But unlike a synchronous call, this call returns immediately, and the app can continue doing other things. When the app needs the results, it makes another call to the service, and the service returns the results. The app can fetch multiple URLs simultaneously, and the URL Fetch service performs the fetches in parallel.

Figure 10-1 shows the sequence of events for a synchronous call and an asynchronous call. As shown, if the fetch is not complete when the app asks for results, the app waits for the remainder of the duration. If the fetch is complete by the time the app asks for results, the service returns the results (or an error message) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required