How To Make ANY Function Asynchronous In Python 3.12

2024 ж. 24 Қаң.
33 298 Рет қаралды

In this tutorial I will be showing you how you can turn any normal function into an asynchronous function in Python. This can be particularly useful with the requests module if you don't want to wait for a request to complete one at a time.
▶ Become job-ready with Python:
www.indently.io
▶ Follow me on Instagram:
/ indentlyreels

Пікірлер
  • I literally just did this and pulled up KZhead because I finished. Crazy ahh timing Specifically I used ansycio to preform multiple network requests.

    @TheBomb420@TheBomb4203 ай бұрын
  • Thanks for such a beautiful and easy explaination sir .

    @mcdolla7965@mcdolla7965Ай бұрын
  • Thanks for the tutorial , I always get stuck on async functions

    @AmineGM73@AmineGM733 ай бұрын
  • Another good video. Thank you, signor :)

    @elatedbento@elatedbento3 ай бұрын
  • I was needed broh! Thank you so much. 💯

    @dipeshsamrawat7957@dipeshsamrawat79573 ай бұрын
    • Glad I could help!

      @Indently@Indently3 ай бұрын
    • @@Indently You are most welcome.

      @dipeshsamrawat7957@dipeshsamrawat79573 ай бұрын
    • Python being a JS ripoff

      @mike.1@mike.13 ай бұрын
    • @@mike.1 Yeah, somehow! Everything that wishes to be connected with the network must look through JS functionalities.

      @dipeshsamrawat7957@dipeshsamrawat79573 ай бұрын
  • There's also the lower level 'loop.run_in_executor' that allows you to specify the 'concurrent.future.Executor' you want to run on. But that's more advanced and should be used only when you really need it ('to_thread' just calls this function, while setting up contextvars)

    @uKaigo@uKaigo3 ай бұрын
    • But prior to Python 3.9, that's what was used, at least to my knowledge

      @uKaigo@uKaigo3 ай бұрын
    • Sounds super interesting! I will read up on that because it sparked my curiosity, thanks for sharing!

      @Indently@Indently3 ай бұрын
  • good video thx you so much plz made threading and threadPool all types and processes also.

    @abdullahsaid4765@abdullahsaid47653 ай бұрын
  • Thanks! Can we archive the same result using thread pool ?

    @ilanbar1970@ilanbar19702 ай бұрын
  • in your example, asynchrony itself is poorly shown, because you did not use gather, and what you did was just run the task in another thread, this has existed since python 3.9

    @h3try@h3tryАй бұрын
  • Great Video! Would you (or anyone else for that matter) mind telling me which text editor you are using?

    @wannich624@wannich6242 ай бұрын
    • Pycharm

      @lyrically6341@lyrically63412 ай бұрын
  • shoud you always use async? or is there specific use cases for it?

    @imusinghuzuni@imusinghuzuni3 ай бұрын
  • Very interesting. Despite Python being my bread and butter, I never had to use it for async/await. When I had the requirement some time ago... I happened to be working in a Go environment, where this sort of thing is arguably one of its strongest suits. What I find even more interesting is that, just as in C#, it's "async all the way up", something I always found a bit annoying when you only have certain parts of your code that needs to be async. There's ways around it, but it requires using the async/await (asyncio) framework with some wrapping code that I always found a bit, I dunno, ugly, I guess.

    @michaelhoffmann2891@michaelhoffmann2891Ай бұрын
  • I did many research on this in past but no one really taught it well and in such simple way. Yeah but I think the tasks should be non interdependent for the functions to works asynchronously.

    @mcdolla7965@mcdolla7965Ай бұрын
  • Naked `dict` type is not the best thing, please specify key/value types or let mypy infer the types.

    @senyai@senyai3 ай бұрын
  • If I am using django, and I need to do some extra work such as run some utility function or something, this can be used there too right?

    @ritankarbhattacharjee7661@ritankarbhattacharjee76613 ай бұрын
    • stick to the "sync_to_async" and "async_to_sync" decorators, it has some event loop and thread pool management baked in.

      @NoProblem76@NoProblem763 ай бұрын
  • What is the difference between asyncio, multithreading and multiprocessing. These things do the same thing, don't they?

    @Smart_Coder7@Smart_Coder73 ай бұрын
    • Multi threading and processing are similar in effect but not necessarily in implementation. Multiprocessing means that code is actually running in parallel on different cores, but multithreading can be done without multiple cores by simply making the different processes wait their turn

      @Slackow@Slackow3 ай бұрын
    • ​@@SlackowThanks

      @Smart_Coder7@Smart_Coder73 ай бұрын
    • I really like a restaurant analogy that I've heard. Your computer has multiple cores and threads, similar to how a restaurant has multiple chefs and waiters. Synchronous code is like accepting only 1 customer at a time, which is handled by 1 waiter and 1 chef. Adding threading is like allowing more customers to come through, with each waiter handling 1 customer. With a language like Python, there's only 1 chef doing the work, but the chef does not need to be actively working on an order during the entire process (set something to cook and do something else). Adding multiprocessing is like hiring more chefs to really increase the amount that can be done in the kitchen, which is mainly useful for restaurants that require the chefs to spend a lot of time on each order opposed to spending most of their time waiting for it to cook. In such cases, adding threading may not even speed up anything because the bottleneck is the chef. Adding asynchronous code is like managing what each component is working on. For example, it's more efficient to have a waiter serve other customers while waiting for an order to finish. This requires more complex code (the drawback of async vs threading), but now you don't have to hire more waiters. Or perhaps your program represents the customers instead of the waiters like in this video. You'll still use threads for each waiter, but with async you can manage multiple customers at the same time. As far as cost analysis, async is the cheapest solution but requires the most effort from the programmer to make effective. Threading is the middle solution, not very expensive and not very complex. Both async and threading can help bring more work to your cores / chefs, which increases the amount of work that can get done as long as your cores / chefs are not the ones actively doing the work (e.g. a web server is the one doing the work for an API request). Multiprocessing is the most expensive solution for when your cores are the one doing the work so you need multiple cores to help out. Hopefully you can see that although each of these techniques can increase the amount of work your program can accomplish at the same time, they all do so in completely different ways, with different advantages and disadvantages. Additionally, it's often not a "single solution" situation. As shown in the video, it's not unusual to use both async and threading in tandem.

      @jacknguyen5220@jacknguyen52202 ай бұрын
  • Just a heads up. Your microphone picks up the vibrations of the table when you type on the keyboard. You can hear it when listening with headset.

    @Johannes@Johannes2 ай бұрын
    • I always had a suspicion that audiophiles would catch onto those things xD My mic-stand is attached to my table, but hopefully someday I'll be able to afford a more elegant solution. Thanks for bringing it up though!

      @Indently@Indently2 ай бұрын
  • We can use celery for seperate thread task na? Sorry if my question is wrong.

    @abdulnafihkt4245@abdulnafihkt42453 ай бұрын
    • Celery, to my understanding, uses processes, not threads. This increases the amount of cores your computer allocates to the program, allowing your computer to do more calculations at a higher overhead cost. However, the example given was an API request. Your computer doesn't do any calculations during the API request, it's just waiting for the web server to do those calculations and send the result back. In this case, it makes a lot more sense to use threading/async.

      @jacknguyen5220@jacknguyen52202 ай бұрын
  • Wasn’t it possible since Python 3.9?

    @artemshumeiko@artemshumeiko3 ай бұрын
    • I'm wondering why did he mentionned python 3.12

      @anthonyraf@anthonyraf3 ай бұрын
    • As KZheadrs we post the latest version to show that it's still relevant. It's been around for a while yes, but maybe it will change in Python 3.15, so including the version I'm currently using is just useful for being specific. In other words, if they deprecate this in Python 3.15 (hypothetically), this tutorial will still stay true to the title for Python 3.12.

      @Indently@Indently3 ай бұрын
    • @@Indently Thanks for the precision : )

      @anthonyraf@anthonyraf3 ай бұрын
    • Seems like a counterlogical argument. Why not just state 3.9 rather than 3.12? Currently it only seems to mention 3.12 for clickbait reasons.

      @thomaskobberpanum1311@thomaskobberpanum13113 ай бұрын
    • Same reason we write "2024" in the year of 2024, yes the code might work also in 2020, but this is the most recent tutorial, and I don't see why I would write Python 3.9 if I'm using Python 3.12? The title is accurate, I don't see why you would click on it unless you want to learn about how to make any function asynchronous, regardless of the Python version.

      @Indently@Indently3 ай бұрын
  • coooooooooool

    @oliverli9630@oliverli96303 ай бұрын
  • @indently Can you pls explain me what is this and why you always use this :- 1.) response:Respone (I mean the colon). 2.) Any function -> Its output's datatype. Waiting for your reply. 😊

    @Smart_Coder7@Smart_Coder73 ай бұрын
    • it s a way to add types in python, variables in python can be of any type, you can initialize a variable as string and later change it to an integer. Adding type with “:” indicates what that variable type should be. Instead the “->” indicates the type of the data returned from the function

      @flavio273@flavio2733 ай бұрын
    • Important to note that it's completely optional and does not affect the program itself in any way. It just helps the developer get the correct type suggestions by the IDE and be able to type check their entire program to make sure everything is working properly.

      @Lanxxe@Lanxxe3 ай бұрын
    • ​@@LanxxeThanks

      @Smart_Coder7@Smart_Coder73 ай бұрын
    • It's called type annotation

      @mahdirasouli6005@mahdirasouli60053 ай бұрын
  • First, pin❤

    @TechnoPlayer64@TechnoPlayer643 ай бұрын
  • More examples

    @hlubradio2318@hlubradio231813 күн бұрын
  • We can do it using threads so why asynchronous?

    @sw-code6027@sw-code60273 ай бұрын
    • My question exactly!

      @cyrilemeka6987@cyrilemeka69873 ай бұрын
    • This IS multi threading. It's just that this way, you can still use async/await as syntactic sugar.

      @SkyyySi@SkyyySi3 ай бұрын
    • @@SkyyySi ohh

      @cyrilemeka6987@cyrilemeka69873 ай бұрын
    • It's important to note that AsyncIO is single threaded.

      @Indently@Indently3 ай бұрын
    • @@Indently Well this information is new for me. Thanks a lot

      @sw-code6027@sw-code60273 ай бұрын
  • Why use asyncio.create_task in your main function and then await the result? Why not just await fetch_status directly?

    @vandelayindustries2971@vandelayindustries29712 ай бұрын
    • The point was that you don't want to wait for 1 request to finish before starting the 2nd request, as he showed in the terminal output toweads the end. That way you can start 2 requests at the same time.

      @jacknguyen5220@jacknguyen52202 ай бұрын
  • When importing a complete module, as you did with asyncio and requests: why bother to then import something else explicit (like Response) instead of referring to it using the already imported module (like requests.Response) as you do with everything else (see requests.get(…) for example)?

    @iham1313@iham13132 ай бұрын
    • It's a personal preference, I hate using dot notation with types.

      @Indently@Indently2 ай бұрын
  • Somehow I prefer "threading"

    @gohilurvish@gohilurvish2 ай бұрын
  • @Indently Why did you make it so complicated and import "Response"? Instead of: import requests from requests import Response response: Response = requests.get() You could simply write: import requests response = requests.get() That would be the same, wouldn't it?

    @seppmaier4858@seppmaier48583 ай бұрын
    • I'm not going to explain it all here, but I recommend researching "the benefits of static type checking". It's what I use in all my code and I couldn't live without it :)

      @Indently@Indently3 ай бұрын
    • ​@@Indently i know that would probably be more complicated, but I think you should also type the generics like those dicts

      @uKaigo@uKaigo3 ай бұрын
    • I'm still working on what would be the most consistent approach considering that some responses return JSON with so many different types, that I kind of feel silly typing it with dict[str, Any], but maybe that would be better for consistency. What are your thoughts?

      @Indently@Indently3 ай бұрын
    • @@Indently if you want to be consistent across videos, I think `dict[str, Any]` would be good. For this video in particular, it would be better as `dict[str, str | int]` since you know that's the correct type. There's also the possibility to use TypedDicts but that's too verbose for a video not focused on it, in my opinion.

      @uKaigo@uKaigo3 ай бұрын
    • Also, you didn't even need to type both apple_status and google_status since their types would be inferred from the task. Unless you want be explicit

      @uKaigo@uKaigo3 ай бұрын
  • Threads for lazy people

    @sorrefly@sorrefly2 ай бұрын
KZhead