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.
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
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.
Become a Member to join the conversation.