How to Create an Async API Call with asyncio

Compared to other programming languages, Python can be much slower at making API calls.
That's because, by design, Python is a blocking language. This means Python code will not be executed until the previous set of instructions has finished. This holds true for definition calls, too.
On the other hand, programming languages like JavaScript are non-blocking languages. That means if a chunk of code is waiting for something to happen, that chunk can release control back to the JavaScript interpreter to do other things.
This considerably improves JavaScript's efficiency because the computer processing JavaScript code isn't idle. Instead, that computer can use its processor cycles for other chunks of code.
Wouldn't it be nice if Python could use async functions, too?
It turns out that Python can use asynchronous code. Though creating callbacks in Python is a little bit more difficult than in JavaScript, a library called asyncio makes writing asynchronous Python code easy. Let's discuss how.
An Intro to asyncio
By design, Python is a blocking language. That means it must finish processing lines of code before it can move on to other tasks. Thankfully, with a library called asyncio, Python can use the same async / await syntax that JavaScript uses to create concurrent code.
Before we go any further, we need to clarify a few things:
First, asyncio has many different uses. This article is not meant as a full tutorial on how to use all its functions. Instead, it is only meant to demonstrate how to use asyncio in Python to write concurrent, non-blocking code. If you want to see all the functions that asyncio can use, it's strongly recommended that you read this library's documentation at Python.org.
Second, we provide two Python code examples below. One example demonstrates how Python uses async /await calls with asyncio. We will reference this set of example syntax throughout the rest of this article. The other example code demonstrates how a traditional Get request is made in Python using the Requests Python library. Again, we will not reference this set of code. It is only included as an example to compare against what asynchronous code looks like in Python.
Third, since we will reference the asynchronous code throughout this article, we will include reference comments in the code example below. For instance, we may refer to 'Reference 1 in the code example.' Therefore, we will notate Reference 1 with a comment in the example code ( # Reference 1).
Fourth, examine the asynchronous Python code example closely. You'll notice the first function is called 'count()' and is located at Reference 1 in the code example. We won't discuss this function at all other than to say its goal is to count once a second and then display that count in the console. This function serves as a visual example of how asynchronous Python code works.
Feel free to copy and run that asynchronous Python code example below. What do you think will happen? When will the response to 'get_delay()' be displayed in the console?
This article was turned into a tutorial. To continue reading and learn how to create an async API call with asyncio, view the tutorial.
delivered to your inbox.
By submitting this form you agree to receive marketing emails from CBT Nuggets and that you have read, understood and are able to consent to our privacy policy.