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 the default subtitles language 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 see our video player troubleshooting guide to resolve the issue.

Completing Asynchronous Support

00:00 Completing Asynchronous Support. In this part of the course, you’ll complete the asynchronous support you started earlier on, starting by adding an async option to the CLI.

00:14 The option can just be a Boolean flag. To implement this kind of option, you use the action argument of the .add_argument() method.

00:22 Open the file and update read_user_cli_args() with the code seen on-screen.

00:32 This call to .add_argument() adds a new -a or --asynchronous option to the application’s CLI. The action argument is set to "store_true", which tells argparse that -a and --asynchronous are Boolean flags that will store True when provided at the command line.

00:52 To check the connectivity of multiple websites asynchronously you’ll write an async function that calls and awaits site_is_online_async() from the checker module.

01:02 Add the code seen on-screen to the file. First, the imports are updated to add asyncio and site_is_online_async().

01:25 Next, _asynchronous_check() is defined as an asynchronous function using the async keyword. This function takes a list of URLs and checks their connectivity asynchronously. Here, you define an inner async function, which allows you to reuse the code that checks a single URL for connectivity.

01:45 Here, you define and initialize a placeholder error variable, which will be used in the call to display_check_result() later. Here, you define a tryexcept statement to wrap the connectivity check. The try block calls and awaits site_is_online_async() with the target URL as an argument. If the call succeeds, then result will be True.

02:09 If the call raises an exception, then result will be False and error will hold the resulting error message.

02:18 This line calls display_check_result() using result, url, and error as arguments. This calls and awaits the gather() function from the asyncio module.

02:30 This function runs a list of awaitable objects concurrently and returns an aggregated list of resulting values if all the awaitable objects complete successfully. To provide the list of awaitable objects, you use a generator expression that calls _check() for each target URL.

02:49 To add the asynchronous functionality to your application’s main() function, you’ll use a conditional statement to check if the user provided the -a or --asynchronous flag at the command line.

03:00 This conditional will allow you to run the connectivity checks with the right tool according to the user’s input. Open the file again, and update the code as seen on-screen.

03:16 The conditional statement checks if the user has provided the -a or --asynchronous flag at the command line. If that’s the case, main() runs the connectivity checks asynchronously using

03:30 Otherwise, it runs the check synchronously using the _synchronous_check() function. That’s it. You can now test this new feature of your website connectivity checker in practice.

03:41 Go back to your command line and run the command seen on-screen. This shows that your application now has an -a or --asynchronous option that will run the connectivity checks asynchronously. Next, the content of the sample-urls file is shown on-screen. Note the order of the websites, starting with

04:04 Next, rpchecker runs the connectivity check synchronously, just like you did in a previous section. This is because you don’t provide the -a or --asynchronous flags. Note that the URLs are checked in the same order that they appear in the text file.

04:20 Now you use the -a flag at the end of the line. This flag makes rpchecker run the connectivity checks concurrently. Now the check results aren’t displayed in the same order as the URLs are entered, but in the order in which the responses come from the target websites.

04:39 As an exercise, run the connectivity checker with a long list of target URLs and compare the execution time when the app runs synchronously and asynchronously.

04:59 Now that you have a fully featured site connectivity checker, in the next section, you’ll take a look back at what you’ve learned in this course.

Become a Member to join the conversation.