Python Asyncio, Requests, Aiohttp | Make faster API Calls
#python #asyncio #aiohttp
Python, asynchronous programming, the event loop... what is all this stuff?
We learn what python is doing in the background so we can make more API Calls faster. We learn how to go from requests to aiohttp in our code, and refactor code for asynchronous programming.
✍️Medium Post: betterprogramming.pub/python-...
⌨️ Code: github.com/PatrickAlphaC/asyn...
✅✅ Donate ✅✅
I use donated funds to spend money on making fun & informational videos.
ETH Wallet address: 0x9680201d9c93d65a3603d2088d125e955c73BD65
😸😸Join the community!😸😸
Chainlink developers discord: / discord
Alpha Chain discord: / discord
Twitter: / alphachainio
Twitter (me): / patrickalphac
Medium: / patrick.collins_58673
Twitch Stream Uploads: / @patrickalphac-alt
Twitch: / patrickalphac
All thoughts and opinions are my own.
"An event loop is when we let some stuff that takes a long time do it's thing in the background. And when it's all set, the 'event loop' will 'pick it up'." is the most helpful and intuitive definition of event loop I've heard.
I really enjoy the fast-pace and the humor of your tutorials. Keeps me from zoning out. You've got a gift for making these tutorials both informative and enjoyable.
Appreciate it! I’m trying to find the right style. I get some people saying they hate it, and others love it. Guess time will tell ahah
@@PatrickAlphaC HA! So true. Well it is definitely different and different is refreshing! I just subscribed. Looking forward to more!
Best async explanation in the burger loving world!!!
🙏🙏🙏
One of the greatest explanations of Async in KZhead.
You’re the man. 13 second code down to half a second. Thank you
you have a skill for making tutorials very engaging. I didn't zone out for a sec
Yay :) Glad it was helpful!
That was an AWESOME tutorial mate. Subscribed!
I really really like this tutorial!! You made it really funny and enjoyable. Never had such a fun programming tutorial on youtube :D Keep going!
Thanks for the comment!
One of the best asyncio tutorial!! Thanks much Patrick!
didnt even zone out once, loving this....SUBSCRIBED
Ty Fren :)
Dude this is the most amusing programming tutorial I've seen in a while! Good stuff, you've got some acting skills
Glad it was fun and educational hehe
@@PatrickAlphaC I wish you made the whole course on that stuff
Oh Dude, I would love to see more tutorials done in this style! Super entertaining!
Very informative. The cooking analogy was gold!
6:12 You could turn any synchronous (i.e. thread-blocking call) into an async one using the to_thread() function available in asyncio with Python 3.9 and later.
hot dog
Dude, you have an amazing way for teaching things that in another way would result boring and complex. You've won a new and loyal subscriptor from Uruguay.
ty fren
Ayo this is the best clip I have ever witnessed about making api calls faster, props to u my man!!
Much obliged friend
Incredible. Thank you so much. In the middle of a development that has 300,000 inevitable API calls. Let me check how it works. Again, Thank you
Good luck!
@@PatrickAlphaC Processed 320k API with 100 APIs at a time. Out of 320k, 91 got connection error (404). When I rerun the remaining APIs, it worked. How to avoid this connection failure? It looks like a common issue in asyncio but dont see proper fix.
For a tricker topic Very Clearly Explained. Thanks brother once again.
wonder i completed 17mins tutorial without getting distracted. Great way of keeping audience on focus :)
i was having a hell of a time understanding async coding and this video really helped. Thank you! Take my sub :)
For more enquiries, reach out⬆️⬆️⬆️..
Great video Patrick! I wonder if you would make similar ones for threading, multi-processing and comparison of all concurrency features.
It`s been less than 1 min of video and I already want to subscribe to his channel. Done.
Thank you .. this changes everything
Fantastic video dude, please share more advanced tips for Python like this!!
I can't click enough on 'like' button. Thanks, your video was amazing!
the best tutorial on the internet, ever
really enjoyed the part where you shuttle between two pans to check if its done and in the process explaining the purpose event loop
Glad that was helpful! I might make this video again...
your video solved my issue! thanks! highly appreciated
Let's gooo!!
Hey Man, Thanks for the video. using Tasks is a super duper and important way
Wow! This is revolutionary technique in teaching how to code, to be honest it is better than the best tutorials here. Keep it up.
Glad it’s fun fren :)
For more enquiries, reach out⬆️⬆️⬆️..
you have nailed it !!!! keep it up.
this is the best tutorial i've watched on youtube. Surprisingly enjoyable for my pea brain
Glad you enjoyed fren :)
the video really cool and fun. and the code examples are really helpful. Thank you!
Loved the tutorial. Thanks 👍
Really Well explained and love the cooking. Subscribed. Very Fun to watch.
Best teacher i've ever seen. Really impressive
Glad it was helpful!
i'm so glad I found you.. Awesomeness !!
Best video on aync I have ever seen. It is just like Tom cruise movie, you have to stick till end, twist may come any moment. Who knows😂
Really loved this explanation!
This is greatest. I bow in front of you Sir!
Who says programmers aren't funny? Great Vid!
Dude please more more more where have you been my whole coding life.
I'm flattered beyond reason
I had to stop the video, to like and comment on this amazing explanation.
Thank you!
Came back to this when exploring MEV and monitoring the chain for certain txs. All the code I've seen uses JS and async/await so was curious on implementing it in python.
Really amazing, so well explained and funny
I didnt come here looking for an amazing channel. but here we are
😍😍😍
Great explanation, thank you! It would be good too if you mention your learning process to grasp different topics
This is a HUGE part of it kzhead.info/sun/fLdulKWjfYWlkpE/bejne.html
Patrick great job. I didn't skip even a second till end. I request you to make more interactive videos on python.
Glad you enjoyed, and I can do that!
I watched many videos and read the docs but understandid the best from you
Glad to hear it :)
good quality video right there. Synthetic and well explained. Good job man
Appreciate it! It’s a pretty tricky topic, so glad to hear it makes sense
this kind of guide, is life-altering. just amazing, man.
Loved the video and the enunciation!
This video makes me hungry and smarter at the same time.
yes
It’s a kind of magic! Thank u!!!
Wow thank you so much Patrick
Two things to remember when to use await. 1. You have to await to register the task to the event loop. 2. You have to await the coroutine to return the 'real response'.
The first job takes significant time, and this is why 11:27 method only diminished 4 seconds. (13s -> 9s) So when we do registering job all at once, it saves time and we could enjoy enormous time gain. (13s->1s)
@@jeffreylim5920 yea that's what he said lol...
Yes we need await before get_task(session)
Finally understood how to implement asycio. Very well explained. You not trying to get everything correct in first attempt and giving errors so viewers can understand why it gave error was something very educational in this video. Subscribed.
As a c++ guy, I can seriously appreciate this
Cooking example was on point lol
Glad you enjoyed
literally i don't feel like i am learning ASYNC, just feel like watching any comedy show..
hmm... thanks for the feedback
@@PatrickAlphaC you really make asyncio easy for me thanks
@@nonesubham oh ahahha
hi, i really loved the tutorial! thanks so much! one question- i have created a different python file, where you use requests, and have imported that to my main file. however, the loop is in my main file. do i add 'async with aiohttp.ClientSession() as session:' in my imported file, and add async def where my loop occurs?
I like your energy and humor
I like you liking it
I'm trying to understand how throwing tasks in an event loop by create_task in tasks list then gather the results(17:02) differs from just executing all tasks at once by gather method(16:38). It seems that gather method not only throws tasks in an event loop but also it executes and waits to be finished(?) so only difference from the two example is that whether you registered the tasks manually by adding create_task or just get them all done by gather method? Did i get it right?
This is exactly what I was looking for. Thankss a ton
Beautiful!
Thanks for sharing ..
Thank you for making this concept so straightforward!
Thank you bro for video so useful
Thanks! I subscribed :D😁
Got a new subscriber. Awesome
12:19 I just ran the exact same code, it says RuntimeError: Event loop is closed. Win11,python 3.10.5
In in the version 2 of your code you have remove the print statments for printing out the each 60 symbol and print is an i/o bound job which indeed reduces the execution time but i am sure with print statment included the execution time gonna be better that the version 1 thanks awsome job :}
Thank you! This is exactly what I needed. Great tutorial
Great video my friend, await more_videos_please
Appropriate joke !
I call you on your Ordinary Sausage "ta da!" Well played
Great video, my only question would be is, how would you make it async if each time you made a call the url changed and there were headers involved in making the api request? Headers being content type and authentication
You'd have to do a lot more conditionals in the code 😅
Your channel is going to blow up. Amazing explanation, keep up the good work
“Blow up” I hope you meant ;)
@@PatrickAlphaC ofcourse! ☺
Great video. would be even better if you could also show how to control number of parallel requests. Uncontrolled parallel requests will trigger dos attack protections and block your further requests.
No, that would be confusing, considering the topic of the tutorial. A step-by-step (keep it simple) approach is preferable.
having that issue now
Dude , thank you for the video , you made this so easy for me to understand how asyncio works , before I could not even begin to understand this concept
Very good! :) Thank you!
Amazing stuff ❤❤
cool I m leraning python the cooking is best analogy
great explanation
Hello, how can we add rate limit in the aiohttp requests using the gather method?
It was very useful thanks dude
Boom!
What if I wanted to create an asynchronous task Loop that executes as tasks are added to it? Say if I am receiving calls from a Discord bot which will access an API and I want to handle those requests asynchronously rather than executing a predefined set of API calls? Additionally, how would I return those results to the original functions which called them? Is that something which is within the purview of aiohttp and asyncio?
Thanks man ✨
nice. thanks for this
give that man a cookie.
This is the best coding tutorial ever! Before finding it i was wondering what half of these things are. Now it's all clear, just like that! Thanks!
Perfect!!
Great stuff!
This was a fun one!
I didn't quite get it - in your last example, do you wait for all the GET requests first and THEN loop over them? Where does the increase in speed come from in this case? Wouldn't it just be the same as the synchronous approach, since you are waiting for all of the responses first before looping over them?
I don't wait for all the GET requests to come back, I just send them all. I then loop over the get their responses. The increase in speed is because I didn't wait for each to respond when sending the GET requests. Does that make sense?
@@PatrickAlphaC oh yeah thanks!
this was awesome 🔥 thank you so much for this great video ❤️🔥😇
🔥🔥🔥
Sir my request is such that, it has to open browser using selenium and then get cookies parameter and then data comes. Currently I use concurrent.futures , which send 7 request at once( coz that's what my PC can take) , so it makes it somewhat fast. Now if I use asyncio, will it make the code faster? For every symbol browser is opened once.
Patrick, Upon doing last task ending at 16:37 minutes. I get connection reset by peer error saying cannot connect to host. I was able to do query API to server till the time we didn't get get_tasks() function defined
You probably don’t have an api key from AV. If you get an api key or use another API you’ll be able to get around the limitations, you’re getting rate limited
amazing way of explanation with nicely chosen examples , you have a great teaching style man
Appreciate it! Hope it was helpful
so great! love from China
love the papa franku cameo
Rip pink guy
this guy is just cool ! ^^
That's great and tnx