Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Checking Sites Asynchronously

00:00 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.

00:26 Python supports asynchronous programming with the asyncio module and the async and 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.

00:41 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.

00:50 This will be the asynchronous equivalent to the site_is_online() function.

00:58 Go back to and add the following code. First you add the required imports of asyncio and aiohttp.

01:14 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.

01:28 This line defines a generic Exception instance as a placeholder. This defines a parser variable containing the result of parsing the target URL using urlparse().

01:41 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.

01:53 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 aiohttp.ClientSession instance.

02:12 This class is the recommended interface for making HTTP requests with aiohttp. These lines define a tryexcept statement.

02:24 The 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 True.

02:37 The first except clause catches TimeoutError exceptions and sets error to a new Exception instance.

02:48 The second except clause catches any other exceptions and updates the error variable accordingly.

02:56 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 site_is_online().

03:08 It returns 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.