Copilot Explains Ep 5: Async/Await in Python
- March 12, 2024
- Posted by: MainInstructor
- Category: BASIC Go Python Visual Basic VSCode
Video Title: Copilot Explains Ep 5: Async/Await in Python
This is Copilot Explains episode 5 Async and Await in Python. You’ll start understanding it soon. Uh explain this to me won’t you. It’s all inside the code. Hey everyone I’m Pamela Fox and I chose to
Talk about Async and Await in Python two constracts that were added back in Python version 3.5. Even though they were added way back in 2015 I only personally started using them in the last year but now I find that I cannot live without them and I really appreciate the benefits they bring
So I thought it’d be a great topic for an explain video. If you want to follow along my GitHub repos right here and now let’s get into it. Here we are inside VS Code and I’ve got this Python
File already open that I’ve started working on. The goal of this Python file is to communicate with the ChatGPT API using the OpenAI SDK so moving down here after we you know set up the authentication to the SDK I’ve got this function here that makes a call to ChatGPT
To ask it to complete this conversation. In this example here what I’m trying to do is something very practical so that I’ve got a four-year-old kid and the four-year-old kid doesn’t want to sleep and just wants to talk about ponies and unicorns and dragons all the time and wants me to make up
Stories and I’m getting a little tired of making up so many stories so I’m going to ask ChatGPT to help me write those stories. For my system prompt I tell it that it’s a you know your tired parent
Trying to get your child to sleep and for my user message I say okay please make up a story about some creature who lives in some location and these are parameters that I send into the
Function and so I send that to ChatGPT and then return the response I try to call it down here so I say response equals generate response let’s go ahead and try to call this line okay well what I immediately see here is a warning coroutine generate response was never awaited this is the
Sort of warning you’re going to see a lot when you start working with Async and Await let’s go ahead and select this and actually ask Copilot to explain it and see if it can give us a good good explanation here so it says that this error message means that the coroutine function was
Defined but not awaited this can happen when a coroutine function is not properly called within a weight statement so let’s just click on this follow-up question here how can I proper call a coroutine function within an Await statement so it says that you need to use the
Wait keyword followed by the coroutine function call and here is an example so it says in your case you need to make sure that the generate response function is defined as a coroutine function and then call it with
Await like this okay so there’s two things we need to do first thing is we need to make sure it’s defined as the coroutine function well what is a coroutine function is that different from a function so here gives this example here it says it’s defined using the Async def syntax so that’s
Actually all we need is Async def as soon as Python sees async def this is now considered to be a co-routine function or you could also just call it a coroutine so it’s a function that is a special kind of function called a coroutine and it must be awaited uh so you
Can see the example here says response equals Await so I’m going to go ahead and put that in here but now I get another error and I actually get this just even with my IntelliSense where it
Says Await statement is outside of a function huh okay this is another issue is that you can’t simply use Await outside of a function you have to call it inside of an Async event loop we can
Even ask Copilot to fix this code for us so I’ll right click on the selected code and say Copilot fix this and Copilot passes in the error messages from my various linters and and then it tries to
Fix it so that it doesn’t have that error take a look at the diff here so it brings in it imports the Async io package which is great that’s from the standard library and that’s a package that can run coroutines in various ways then it wraps this call inside another coroutine Async def
It prints out something from the response and then finally it uses async io dot run to call this coroutine so I’m going to accept this and then I’m just going to make some tweaks so I’m going
To bring this import up to the top just to keep everything sorted and clean then I’m also going to just print out the entire response so I can see all of it let’s give it a run okay so that
Worked let’s check out this story we can see it inside this chat completion object and we can see there was a dragon named Draco fearsome scales and it looks like maybe he became friendly and he had some tea and the dragon was never alone again again. Ah I love those stories all right
Thank you ChatGPT. You might be thinking what’s the benefit of Async why put in so much effort to call a function well we need Async coroutines if we want concurrency the ability to pause one coroutine while it’s waiting for io and then start up another coroutine it’s particularly helpful
When our io calls are quite slow like ChatGPT calls that can take several seconds we can try to demonstrate that in this simple example by making multiple ChatGPT calls concurrently let me go ahead and ask Chat can I call multiple Async coroutines at once we’ll just say that okay it
Says I can do this using Async io dot gather it takes in multiple coroutines as arguments and returns a feature that completes when all the coroutines have completed and we see a nice little example
Here let’s go ahead and see if Copilot can do this so I’m going to start a code chat and say call generate response multiple times using Async io dot gather and print out the results let’s see
If we can do that that looks good so I’ll accept let’s try this out and see if we can get three stories okay I definitely see a mermaid here cuz there’s Marina I see a dragon and I see a unicorn
So we saw the Unicorn story then the Dragon Story then the mermaid story so they came out in the same order I asked but that doesn’t necessarily always have to be the case that they come out in
The same order so I’m going to go ahead and put some print statements in here so we can track what’s going on uh so we’re going to say generate a response for creature in location just F string
That and then I want to know when it ends so I’m going to say print got response for creature in location and F string that okay cuz what I want to show is that there it is actually happening
At the same time these calls let’s run this again so what you can see is that it you know sent off these requests all in a sequence here so it first sent off the Unicorn request and then it sent
Off the dragon request before getting a response for the Unicorn so that’s really interesting cuz that’s not how it would worked if these were you know normal functions uh so it sent off all these requests and then it got back the request and you actually see this time it got back the requests
In a different order so we see the dragon then the mermaid then the unicorn and that’s something interesting that can happen when you’re using Async and Await is that you know things could get fulfilled in different orders uh so this something to keep in mind if you actually were assuming some
Sort of sequence and that’s what’s really really cool about Async and Await is the ability to you know send off multiple things to have concurrency especially when we’re writing user facing web apps to make a web app in Python that supports Async we need to use a Async compatible web framework
Like FastAPI one way you could get started with FastAPI is to ask Copilot to create a workspace for you so slash create workspace for a simple FastAPI app with a single route here Copilot proposes a directory structure for a simple Fast API app and also showcases some of the code so
You can see here is what it looks like to have an app with a single route and this is where I would put the call to generate the actual story and I would change this to take some parameters
And then it even generated some tests which I love because I think that all apps should be tested so we could actually click this button here create work workspace and it would create this new
Workspace here and you can see it opened up in a brand new VS Code window so I could start working here on my FastAPI app to call asynchronous APIs I hope you have a better understanding of Async and Await after this video if you have any Async resources that have helped you please
Post them in the chat or you could even post your own explanation of Async I am still learning more every day just yesterday I discovered a feature added to python 3.11 called task group it’s an alternative to gather and I used it to asynchronously make 100 concurrent calls so cool let’s keep learning
[Music] together
Video Keywords: Visual Basic
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
Great video and code. Just what I was looking for. One thing I noticed is that when I adapted the code to my use case the responses aren't available until all the requests completed. Is there a way to access each response when it is returned?
strage python in the left background side:)
Very cool!
Great intro – thanks! I loved the kid story example. Brings back some nice memories 🙂