How to Create an Async API Call with asyncio | Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
it's all well and good to talk about synchronous and asynchronous programming but i think it's helpful to see it actually in action to understand how we can use it to benefit the responsiveness of our software so i'm going to do that with some python code you can see my beginning code right here and this is going to rely on something called httpbin.org if i bring up that website you can see here that all this is is a publicly available site that allows me to hit some different endpoints so i can test out my http calls in particular i'm interested in one underneath the dynamic data section here which is get forward slash delay forward slash a number which is going to return a delayed response so for example if i go to http bin dot org forward slash delay forward slash six well my browser is going to wait it's going to take six seconds and then i'm going to get the response back from that website which is this nice json document with some data about what just went on well we're going to do exactly that here in this code this first code is a synchronous piece of python code and you can see here i hit that same base url that's what's in my base url function i'm using the requests library which is python's built-in http request library and i define a function where i take the number of seconds i build an endpoint with that number in it so it's going to be forward slash delay forward slash in this case 5 is the number that i'm going to pass into it i call that url right here and when it returns i print out the data it's pretty straightforward stuff if you are familiar with python this shouldn't be too unusual for you i'm going to call python sync dot pi and we can see it's getting with a five delay i'm just going to wait a few seconds probably takes about five seconds to do and then i'm gonna get that json response back from the server so and there we can see at the bottom is okay all finished so it went out it hit the server once the server responded it printed that data and i got my completed message this is great right but this is a synchronous call what if i wanted some kind of like timer or countdown going on while i was waiting for that or what if i wanted just the interface of my program my software to be open and available while i was waiting for that response to come back from the server what could i do i couldn't do anything because this code was blocked while it waited by the request library it was blocked for the response from the server so instead let's enter the asynchronous version of this now asynchronous calls in python are a little more complex python is a blocking language kind of by default intrinsically if we were doing this in say javascript the implementation is much easier and much more simple because javascript is a non-blocking language intrinsically and it handles these things better but using something called the async io library we can create asynchronous calls in python and using the aio http library which i installed with pip we can create asynchronous http calls i cannot teach you all about asynchronous and weight in this video it's a pretty big topic and it would take quite a bit of time i'm just going to go over loosely how this code works first of all using the async keyword i define this count method and you can see it's going to count through five and between each count is going to call this async io.sleep method with the keyword await this is the the critical function for creating asynchronous methods inside of python so this is an asynchronous counting method while this method is counting and it's awaiting a response from the sleep method it can go do other things i've also got my get delay function here which looks very very similar to the get delay function for my synchronous column it takes seconds as a parameter it builds a url based on that but instead of using the request library i use the aio http library so i use async width here to create an instance of my client session object and then i use async width here to actually execute the get on my client session object and get the response back and then my response equals response.read again with await so while i am waiting for response.read to finish while i'm waiting for the server to respond i can do other things this code can release control back to something else lastly i use an async def main method right here again i use await async io.gather is the function here and i pass in my two functions getdelay and count i know i get it that's a lot the point is i've used all the correct keywords in all the correct places and what this is going to do is these two functions are going to run asynchronously or simultaneously with one another so first while getdelay is running and waiting for a response the count function will fire off so let's see how this actually works while you're in action this is going to be python async dot pi and it looks the same getting with five delay but now it's counting so while it's waiting for the response it's executing that count function and what happens when it gets to the end one two three four five now i get my json back from the client session right so while client session was waiting for that response down here it was able to execute the second function here inside of my async io.gather list and the same thing is true the reverse way right my count is an asynchronous function as well so let's imagine that i did this what if i said 678 then i saved this so now my count is going to go up to eight seconds what's different about that well i should expect my json to come back in the middle if we watch this five six there okay it came after six not after five um you know there's extra delay in the transmission over the internet whatever so i got up to six seconds and then my get delay function came back so while it was awaiting for the async io.sleep right here the count function said hey okay i cannot take a break and someone else can run and the getdelay function did its print command down here before returning control back to the count method so that is how you do asynchronous calls in python and you can see some immediate advantages to creating asynchronous calls they can run concurrently and they keep from blocking or locking up your user interface while you might be waiting for a long response from another server i hope this has been informative for you and i'd like to thank you for viewing
Info
Channel: CBT Nuggets
Views: 24,162
Rating: undefined out of 5
Keywords: cbt nuggets, it training, it jobs, it certification, it learning, it training program, tech learning, tech training, certification training, training courses, it career, career progression, it tutorial, e-learning
Id: t0JXiljpNRo
Channel Id: undefined
Length: 6min 0sec (360 seconds)
Published: Mon Feb 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.