Checking Sites Asynchronously
Checking Sites Asynchronously. By performing the connectivity checks on multiple websites concurrently through asynchronous programming, you can improve the overall performance of your application. To do this, you can take advantage of Python’s asynchronous features and the
aiohttp third-party library, which you’ve already installed in your project’s virtual environment.
Python supports asynchronous programming with the
asyncio module and the
await keywords. In this section of the course, you’ll write the required code to make your app run the connectivity checks asynchronously using these tools.
The first step of making the checker work concurrently is to write an
async function that allows you to perform a single connectivity check on a given website.
This will be the asynchronous equivalent to the
Go back to
checker.py and add the following code. First you add the required imports of
Next you define
site_is_online_async(). It’s an
async function that takes two arguments, the URL to check and the number of seconds before the requests time out.
This line defines a generic
Exception instance as a placeholder. This defines a
parser variable containing the result of parsing the target URL using
This line uses the
or operator to extract the hostname from the target URL. Next, you define a
for loop over the HTTP and HTTPS schemes.
This will allow you to check if the website is available in either one. This uses a string to build a URL using the current scheme and the hostname. Next, you define an
async with statement to handle an
This class is the recommended interface for making HTTP requests with
aiohttp. These lines define a
try block performs and awaits a
HEAD request to the target website by calling
.head() on the
session object. If the request succeeds, then the function returns
except clause catches
TimeoutError exceptions and sets
error to a new
except clause catches any other exceptions and updates the
error variable accordingly.
This last line raises the exception stored in
error if the loop finishes without a successful request. This asynchronous function is similar to the implementation of
True if the target website is online. Otherwise, it raises an exception pointing out the encountered problem. The main difference between these function is that
site_is_online_async() performs the HTTP requests asynchronously using the
aiohttp third-party library. As already mentioned, this can help you optimize your app’s performance when you have a long list of websites to check.
03:34 In the next section of the course, you’ll write the code needed to complete the implementation of asynchronous checks.
Become a Member to join the conversation.