Python Asyncio, Requests, Aiohttp | Make faster API Calls

2021 ж. 19 Сәу.
119 940 Рет қаралды

#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.

    @saminyead1233@saminyead12338 ай бұрын
  • 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.

    @werthersoriginal@werthersoriginal2 жыл бұрын
    • 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@PatrickAlphaC2 жыл бұрын
    • @@PatrickAlphaC HA! So true. Well it is definitely different and different is refreshing! I just subscribed. Looking forward to more!

      @werthersoriginal@werthersoriginal2 жыл бұрын
  • Best async explanation in the burger loving world!!!

    @ubaidulkhan@ubaidulkhan2 жыл бұрын
    • 🙏🙏🙏

      @PatrickAlphaC@PatrickAlphaC2 жыл бұрын
  • One of the greatest explanations of Async in KZhead.

    @ermalgashimramori@ermalgashimramori Жыл бұрын
  • You’re the man. 13 second code down to half a second. Thank you

    @apexlex1634@apexlex163412 күн бұрын
  • you have a skill for making tutorials very engaging. I didn't zone out for a sec

    @davidkorn5253@davidkorn5253 Жыл бұрын
    • Yay :) Glad it was helpful!

      @PatrickAlphaC@PatrickAlphaC Жыл бұрын
  • That was an AWESOME tutorial mate. Subscribed!

    @padraic1983@padraic19832 жыл бұрын
  • 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!

    @nam4574@nam45742 жыл бұрын
    • Thanks for the comment!

      @PatrickAlphaC@PatrickAlphaC2 жыл бұрын
  • One of the best asyncio tutorial!! Thanks much Patrick!

    @kapilsonyt@kapilsonyt2 жыл бұрын
  • didnt even zone out once, loving this....SUBSCRIBED

    @FaizanUlHaq-mf3zt@FaizanUlHaq-mf3zt2 ай бұрын
    • Ty Fren :)

      @PatrickAlphaC@PatrickAlphaCАй бұрын
  • Dude this is the most amusing programming tutorial I've seen in a while! Good stuff, you've got some acting skills

    @vadim921@vadim9212 жыл бұрын
    • Glad it was fun and educational hehe

      @PatrickAlphaC@PatrickAlphaC2 жыл бұрын
    • @@PatrickAlphaC I wish you made the whole course on that stuff

      @vadim921@vadim9212 жыл бұрын
  • Oh Dude, I would love to see more tutorials done in this style! Super entertaining!

    @Lorant1984@Lorant1984 Жыл бұрын
  • Very informative. The cooking analogy was gold!

    @billjellesma1371@billjellesma1371 Жыл бұрын
  • 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.

    @lawrencedoliveiro9104@lawrencedoliveiro9104 Жыл бұрын
    • hot dog

      @PatrickAlphaC@PatrickAlphaC Жыл бұрын
  • 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.

    @903vulture@903vulture Жыл бұрын
    • ty fren

      @PatrickAlphaC@PatrickAlphaC Жыл бұрын
  • Ayo this is the best clip I have ever witnessed about making api calls faster, props to u my man!!

    @evanfonseka5520@evanfonseka55202 жыл бұрын
    • Much obliged friend

      @PatrickAlphaC@PatrickAlphaC2 жыл бұрын
  • 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

    @venkatramachandran6345@venkatramachandran63452 жыл бұрын
    • Good luck!

      @PatrickAlphaC@PatrickAlphaC2 жыл бұрын
    • @@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.

      @venkatramachandran6345@venkatramachandran63452 жыл бұрын
  • For a tricker topic Very Clearly Explained. Thanks brother once again.

    @vickysharma9227@vickysharma9227 Жыл бұрын
  • wonder i completed 17mins tutorial without getting distracted. Great way of keeping audience on focus :)

    @bravitheja1142@bravitheja1142Ай бұрын
  • i was having a hell of a time understanding async coding and this video really helped. Thank you! Take my sub :)

    @carterreynolds5649@carterreynolds5649 Жыл бұрын
    • For more enquiries, reach out⬆️⬆️⬆️..

      @kovychchat__my_Teleqram_id@kovychchat__my_Teleqram_id Жыл бұрын
  • Great video Patrick! I wonder if you would make similar ones for threading, multi-processing and comparison of all concurrency features.

    @Flyingnobull@Flyingnobull Жыл бұрын
  • It`s been less than 1 min of video and I already want to subscribe to his channel. Done.

    @paulovinicius5833@paulovinicius58332 жыл бұрын
  • Thank you .. this changes everything

    @BillThornton-pm4hd@BillThornton-pm4hd8 ай бұрын
  • Fantastic video dude, please share more advanced tips for Python like this!!

    @Woeden@Woeden Жыл бұрын
  • I can't click enough on 'like' button. Thanks, your video was amazing!

    @lucasvazdelima4420@lucasvazdelima4420 Жыл бұрын
  • the best tutorial on the internet, ever

    @Nikki713100@Nikki7131002 жыл бұрын
  • really enjoyed the part where you shuttle between two pans to check if its done and in the process explaining the purpose event loop

    @anoopisaac@anoopisaac Жыл бұрын
    • Glad that was helpful! I might make this video again...

      @PatrickAlphaC@PatrickAlphaC Жыл бұрын
  • your video solved my issue! thanks! highly appreciated

    @stro3277@stro3277 Жыл бұрын
    • Let's gooo!!

      @PatrickAlphaC@PatrickAlphaC Жыл бұрын
  • Hey Man, Thanks for the video. using Tasks is a super duper and important way

    @vickysharma9227@vickysharma9227 Жыл бұрын
  • Wow! This is revolutionary technique in teaching how to code, to be honest it is better than the best tutorials here. Keep it up.

    @RenormalizedAdvait@RenormalizedAdvait Жыл бұрын
    • Glad it’s fun fren :)

      @PatrickAlphaC@PatrickAlphaC Жыл бұрын
    • For more enquiries, reach out⬆️⬆️⬆️..

      @kovychchat__my_Teleqram_id@kovychchat__my_Teleqram_id Жыл бұрын
  • you have nailed it !!!! keep it up.

    @smitjainsj@smitjainsj2 жыл бұрын
  • this is the best tutorial i've watched on youtube. Surprisingly enjoyable for my pea brain

    @mattmaximo8716@mattmaximo87162 жыл бұрын
    • Glad you enjoyed fren :)

      @PatrickAlphaC@PatrickAlphaC2 жыл бұрын
  • the video really cool and fun. and the code examples are really helpful. Thank you!

    @peterwoo2718@peterwoo27182 ай бұрын
  • Loved the tutorial. Thanks 👍

    @nassehk@nassehk6 ай бұрын
  • Really Well explained and love the cooking. Subscribed. Very Fun to watch.

    @akashsoren1368@akashsoren1368 Жыл бұрын
  • Best teacher i've ever seen. Really impressive

    @yerielkim327@yerielkim3272 жыл бұрын
    • Glad it was helpful!

      @PatrickAlphaC@PatrickAlphaC2 жыл бұрын
  • i'm so glad I found you.. Awesomeness !!

    @EekaMau5@EekaMau5 Жыл бұрын
  • 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😂

    @asimkhan-ko6ew@asimkhan-ko6ew8 ай бұрын
  • Really loved this explanation!

    @damiyagondha7153@damiyagondha7153Ай бұрын
  • This is greatest. I bow in front of you Sir!

    @Kaustubhingale952@Kaustubhingale952 Жыл бұрын
  • Who says programmers aren't funny? Great Vid!

    @hypebeastuchiha9229@hypebeastuchiha92292 жыл бұрын
  • Dude please more more more where have you been my whole coding life.

    @h8handles@h8handles2 жыл бұрын
    • I'm flattered beyond reason

      @PatrickAlphaC@PatrickAlphaC2 жыл бұрын
  • I had to stop the video, to like and comment on this amazing explanation.

    @malakggh@malakggh Жыл бұрын
    • Thank you!

      @PatrickAlphaC@PatrickAlphaC Жыл бұрын
  • 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.

    @legateau5387@legateau5387 Жыл бұрын
  • Really amazing, so well explained and funny

    @eugenmalatov5470@eugenmalatov547018 күн бұрын
  • I didnt come here looking for an amazing channel. but here we are

    @idlenoodle2385@idlenoodle23852 жыл бұрын
    • 😍😍😍

      @PatrickAlphaC@PatrickAlphaC2 жыл бұрын
  • Great explanation, thank you! It would be good too if you mention your learning process to grasp different topics

    @ahmed-samer@ahmed-samer11 ай бұрын
    • This is a HUGE part of it kzhead.info/sun/fLdulKWjfYWlkpE/bejne.html

      @PatrickAlphaC@PatrickAlphaC11 ай бұрын
  • Patrick great job. I didn't skip even a second till end. I request you to make more interactive videos on python.

    @sujalpadhiyar5731@sujalpadhiyar5731 Жыл бұрын
    • Glad you enjoyed, and I can do that!

      @PatrickAlphaC@PatrickAlphaC Жыл бұрын
  • I watched many videos and read the docs but understandid the best from you

    @kareemmahlees3597@kareemmahlees3597 Жыл бұрын
    • Glad to hear it :)

      @PatrickAlphaC@PatrickAlphaC Жыл бұрын
  • good quality video right there. Synthetic and well explained. Good job man

    @rolandandrews7518@rolandandrews75182 жыл бұрын
    • Appreciate it! It’s a pretty tricky topic, so glad to hear it makes sense

      @PatrickAlphaC@PatrickAlphaC2 жыл бұрын
  • this kind of guide, is life-altering. just amazing, man.

    @luxeave@luxeave2 жыл бұрын
  • Loved the video and the enunciation!

    @ravindrakakade9859@ravindrakakade9859 Жыл бұрын
  • This video makes me hungry and smarter at the same time.

    @Networkprofessor@Networkprofessor Жыл бұрын
    • yes

      @PatrickAlphaC@PatrickAlphaC Жыл бұрын
  • It’s a kind of magic! Thank u!!!

    @filippov_es@filippov_es2 жыл бұрын
  • Wow thank you so much Patrick

    @dumisanimthethwa4279@dumisanimthethwa4279 Жыл бұрын
  • 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'.

    @jeffreylim5920@jeffreylim59202 жыл бұрын
    • 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@jeffreylim59202 жыл бұрын
    • @@jeffreylim5920 yea that's what he said lol...

      @carterreynolds5649@carterreynolds5649 Жыл бұрын
    • Yes we need await before get_task(session)

      @zmaktr@zmaktr Жыл бұрын
  • 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.

    @127.@127.2 жыл бұрын
  • As a c++ guy, I can seriously appreciate this

    @andyhawaii2819@andyhawaii2819 Жыл бұрын
  • Cooking example was on point lol

    @Tntpker@Tntpker2 жыл бұрын
    • Glad you enjoyed

      @PatrickAlphaC@PatrickAlphaC2 жыл бұрын
  • literally i don't feel like i am learning ASYNC, just feel like watching any comedy show..

    @nonesubham@nonesubham11 ай бұрын
    • hmm... thanks for the feedback

      @PatrickAlphaC@PatrickAlphaC11 ай бұрын
    • @@PatrickAlphaC you really make asyncio easy for me thanks

      @nonesubham@nonesubham11 ай бұрын
    • @@nonesubham oh ahahha

      @PatrickAlphaC@PatrickAlphaC10 ай бұрын
  • 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?

    @ishikadav1708@ishikadav17089 ай бұрын
  • I like your energy and humor

    @danielhidefjall5060@danielhidefjall50602 жыл бұрын
    • I like you liking it

      @PatrickAlphaC@PatrickAlphaC2 жыл бұрын
  • 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?

    @user-up1xf2lq4y@user-up1xf2lq4y2 жыл бұрын
  • This is exactly what I was looking for. Thankss a ton

    @SaishShettytgroxx@SaishShettytgroxx2 жыл бұрын
    • Beautiful!

      @PatrickAlphaC@PatrickAlphaC2 жыл бұрын
  • Thanks for sharing ..

    @45234523452345@452345234523456 ай бұрын
  • Thank you for making this concept so straightforward!

    @ziyiyang4550@ziyiyang45502 жыл бұрын
  • Thank you bro for video so useful

    @yerkebulanzhaukenov138@yerkebulanzhaukenov1382 жыл бұрын
  • Thanks! I subscribed :D😁

    @sevashpun@sevashpun2 жыл бұрын
  • Got a new subscriber. Awesome

    @jbusmoove@jbusmoove6 ай бұрын
  • 12:19 I just ran the exact same code, it says RuntimeError: Event loop is closed. Win11,python 3.10.5

    @dipankardey1044@dipankardey1044 Жыл бұрын
  • 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 :}

    @geekydanish5990@geekydanish5990 Жыл бұрын
  • Thank you! This is exactly what I needed. Great tutorial

    @Whoosh12345@Whoosh123452 жыл бұрын
  • Great video my friend, await more_videos_please

    @redjinnee2008@redjinnee20082 жыл бұрын
    • Appropriate joke !

      @PatrickAlphaC@PatrickAlphaC2 жыл бұрын
  • I call you on your Ordinary Sausage "ta da!" Well played

    @AR-fh2uh@AR-fh2uh2 жыл бұрын
  • 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

    @spencerphill8115@spencerphill81155 ай бұрын
    • You'd have to do a lot more conditionals in the code 😅

      @PatrickAlphaC@PatrickAlphaC5 ай бұрын
  • Your channel is going to blow up. Amazing explanation, keep up the good work

    @safeer786ful@safeer786ful2 жыл бұрын
    • “Blow up” I hope you meant ;)

      @PatrickAlphaC@PatrickAlphaC2 жыл бұрын
    • @@PatrickAlphaC ofcourse! ☺

      @safeer786ful@safeer786ful2 жыл бұрын
  • 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.

    @anamoly01@anamoly01 Жыл бұрын
    • No, that would be confusing, considering the topic of the tutorial. A step-by-step (keep it simple) approach is preferable.

      @Lorant1984@Lorant1984 Жыл бұрын
    • having that issue now

      @oakae326@oakae326 Жыл бұрын
  • 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

    @PrakashReddyK@PrakashReddyK Жыл бұрын
  • Very good! :) Thank you!

    @galichandreyschool@galichandreyschool Жыл бұрын
  • Amazing stuff ❤❤

    @h00t-h00t@h00t-h00t9 ай бұрын
  • cool I m leraning python the cooking is best analogy

    @dinimueterihrhurene2772@dinimueterihrhurene2772 Жыл бұрын
  • great explanation

    @raghavarahulkalavala1542@raghavarahulkalavala1542 Жыл бұрын
  • Hello, how can we add rate limit in the aiohttp requests using the gather method?

    @leoparrilha1@leoparrilha111 ай бұрын
  • It was very useful thanks dude

    @sheracore9190@sheracore9190 Жыл бұрын
    • Boom!

      @PatrickAlphaC@PatrickAlphaC Жыл бұрын
  • 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?

    @BaronXOfficial@BaronXOfficial2 жыл бұрын
  • Thanks man ✨

    @Ji8@Ji8 Жыл бұрын
  • nice. thanks for this

    @rkiacnhg@rkiacnhg Жыл бұрын
  • give that man a cookie.

    @sahabtariq4039@sahabtariq40392 жыл бұрын
  • 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!

    @AD-cw1ny@AD-cw1ny2 жыл бұрын
    • Perfect!!

      @PatrickAlphaC@PatrickAlphaC2 жыл бұрын
  • Great stuff!

    @EldorJ10@EldorJ10 Жыл бұрын
  • This was a fun one!

    @GurashishSingh1@GurashishSingh1 Жыл бұрын
  • 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?

    @user-dx2yp1rk4k@user-dx2yp1rk4k2 жыл бұрын
    • 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@PatrickAlphaC2 жыл бұрын
    • @@PatrickAlphaC oh yeah thanks!

      @user-dx2yp1rk4k@user-dx2yp1rk4k2 жыл бұрын
  • this was awesome 🔥 thank you so much for this great video ❤️🔥😇

    @ankitbrijwasi9902@ankitbrijwasi99022 жыл бұрын
    • 🔥🔥🔥

      @PatrickAlphaC@PatrickAlphaC2 жыл бұрын
  • 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.

    @tushar3875@tushar38752 жыл бұрын
  • 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

    @ParthPatel-jn6io@ParthPatel-jn6io2 жыл бұрын
    • 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

      @PatrickAlphaC@PatrickAlphaC2 жыл бұрын
  • amazing way of explanation with nicely chosen examples , you have a great teaching style man

    @fuad471@fuad4712 жыл бұрын
    • Appreciate it! Hope it was helpful

      @PatrickAlphaC@PatrickAlphaC2 жыл бұрын
  • so great! love from China

    @lapraschaos5152@lapraschaos5152 Жыл бұрын
  • love the papa franku cameo

    @meodudu6188@meodudu61882 жыл бұрын
    • Rip pink guy

      @PatrickAlphaC@PatrickAlphaC2 жыл бұрын
  • this guy is just cool ! ^^

    @tungleanh9993@tungleanh99932 жыл бұрын
  • That's great and tnx

    @hassanyahyaei2882@hassanyahyaei28822 жыл бұрын
KZhead