Making HTTP requests to the Tornado non-blocking API

Now, we can run the async_api.py script that launches Tornados's development server to compose and send HTTP requests to our new version of the Web API that uses the non-blocking features of Tornado combined with asynchronous execution. Execute the following command:

python async_api.py

The following lines show the output after we execute the previous command. The Tornado HTTP development server is listening at port 8888:

Listening at port 8888

With the previous command, we will start the Tornado HTTP server and it will listen on every interface on port 8888. Thus, if we want to make HTTP requests to our API from other computers or devices connected to our LAN, we don't need any additional configurations.

In our new version of the API, each HTTP request is non-blocking. Thus, whenever the Tornado HTTP server receives an HTTP request and makes an asynchronous call, it is able to start working on any other HTTP request in the incoming queue before the server sends the response for the first HTTP request it received. The methods we coded in the request handlers are working with an asynchronous execution and they take advantage of the non-blocking features included in Tornado, combined with asynchronous executions.

In order to set the brightness level for both the blue and white LEDs, we have to make two HTTP PATCH requests. We will make them to understand how our new version of the API processes two incoming requests.

Open two Cygwin terminals in Windows, or two Terminals in macOS or Linux, and write the following command in the first one. We will compose and send an HTTP request to set the brightness level for the blue LED to 255. Write the line in the first window but don't press Enter yet, as we will try to launch two commands at almost the same time in two windows:

http PATCH :8888/leds/1 brightness_level=255

The following is the equivalent curl command:

curl -iX PATCH -H "Content-Type: application/json" -d 
    '{"brightness_level":255}' :8888/leds/1

Now, go to the second window and write the following command. We will compose and send an HTTP request to set the brightness level for the white LED to 255. Write the line in the second window but don't press Enter yet, as we will try to launch two commands at almost the same time in two windows:

http PATCH :8888/leds/2 brightness_level=255

The following is the equivalent curl command:

curl -iX PATCH -H "Content-Type: application/json" -d '{"brightness_level":255}' :8888/leds/2

Now, go to the first window, press Enter . Then, go to the second window and quickly press Enter. You will see the following lines in the window that is running the Tornado HTTP server:

I've started setting the Blue LED's brightness level
I've started setting the White LED's brightness level

Then, you will see the following lines that show the results of executing the print statements that describe when the code finished setting the brightness level for the LEDs:

I've finished setting the Blue LED's brightness level
I've finished setting the White LED's brightness level

The server could start processing the request that changes the brightness level for the white LED before the request that changes the brightness level of the blue LED finishes its execution. The following screenshot shows three windows on Windows. The window on the left-hand side is running the Tornado HTTP server and displays the messages printed in the methods that process the HTTP requests. The window on the upper-right corner is running the http command to generate the HTTP request that changes the brightness level for the blue LED. The window at the lower-right corner is running the http command to generate the HTTP request that changes the brightness level for the white LED. It is a good idea to use a similar configuration to check the output while we compose and send the HTTP requests and check how the asynchronous execution is working on the new version of the API:

Making HTTP requests to the Tornado non-blocking API

Each operation takes some time but doesn't block the possibility to process other incoming HTTP requests thanks to the changes we made to the API to take advantage of the asynchronous execution. This way, it is possible to change the brightness level for the white LED while the other request is to change the brightness level for the blue LED. Tornado is able to start processing requests while the I/O operations with the drone take some time to complete.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.253.223