4 Concurrent web requests

 

This chapter covers

  • Asynchronous context managers
  • Making asyncio-friendly web requests with aiohttp
  • Running web requests concurrently with gather
  • Processing results as they come in with as completed
  • Keeping track of in-flight requests with wait
  • Setting and handling timeouts for groups of requests and canceling requests

In chapter 3, we learned more about the inner workings of sockets and built a basic echo server. Now that we’ve seen how to design a basic application, we’ll take this knowledge and apply it to making concurrent, non-blocking web requests. Utilizing asyncio for web requests allows us to make hundreds of them at the same time, cutting down on our application’s runtime compared to a synchronous approach. This is useful for when we must make multiple requests to a set of REST APIs, as can happen in a microservice architecture or when we have a web crawling task. This approach also allows for other code to run as we’re waiting for potentially long web requests to finish, allowing us to build more responsive applications.

4.1 Introducing aiohttp

4.2 Asynchronous context managers

4.2.1 Making a web request with aiohttp

4.2.2 Setting timeouts with aiohttp

4.3 Running tasks concurrently, revisited

4.4 Running requests concurrently with gather

4.4.1 Handling exceptions with gather

4.5 Processing requests as they complete

4.5.1 Timeouts with as_completed

4.6 Finer-grained control with wait

4.6.1 Waiting for all tasks to complete

4.6.2 Watching for exceptions

4.6.3 Processing results as they complete

4.6.4 Handling timeouts

4.6.5 Why wrap everything in a task?

Summary

sitemap