How To Make ANY Function Asynchronous In Python 3.12

Поделиться
HTML-код
  • Опубликовано: 24 янв 2024
  • 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

Комментарии • 81

  • @TheBomb420
    @TheBomb420 4 месяца назад +16

    I literally just did this and pulled up RUclips because I finished. Crazy ahh timing
    Specifically I used ansycio to preform multiple network requests.

  • @elatedbento
    @elatedbento 4 месяца назад

    Another good video. Thank you, signor :)

  • @mcdolla7965
    @mcdolla7965 3 месяца назад

    Thanks for such a beautiful and easy explaination sir .

  • @uKaigo
    @uKaigo 4 месяца назад +7

    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
      @uKaigo 4 месяца назад +3

      But prior to Python 3.9, that's what was used, at least to my knowledge

    • @Indently
      @Indently  4 месяца назад +3

      Sounds super interesting! I will read up on that because it sparked my curiosity, thanks for sharing!

  • @dipeshsamrawat7957
    @dipeshsamrawat7957 4 месяца назад +4

    I was needed broh! Thank you so much. 💯

    • @Indently
      @Indently  4 месяца назад +2

      Glad I could help!

    • @dipeshsamrawat7957
      @dipeshsamrawat7957 4 месяца назад +1

      @@Indently You are most welcome.

    • @mike.1
      @mike.1 4 месяца назад

      Python being a JS ripoff

    • @dipeshsamrawat7957
      @dipeshsamrawat7957 4 месяца назад

      @@mike.1 Yeah, somehow! Everything that wishes to be connected with the network must look through JS functionalities.

  • @AmineGM73
    @AmineGM73 4 месяца назад +4

    Thanks for the tutorial , I always get stuck on async functions

  • @ilanbar1970
    @ilanbar1970 4 месяца назад +2

    Thanks!
    Can we archive the same result using thread pool ?

  • @abdullahsaid4765
    @abdullahsaid4765 4 месяца назад +1

    good video thx you so much plz made threading and threadPool all types and processes also.

  • @h3try
    @h3try 2 месяца назад +1

    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

  • @wannich624
    @wannich624 4 месяца назад

    Great Video! Would you (or anyone else for that matter) mind telling me which text editor you are using?

  • @imusinghuzuni
    @imusinghuzuni 4 месяца назад

    shoud you always use async? or is there specific use cases for it?

  • @artemshumeiko
    @artemshumeiko 4 месяца назад +27

    Wasn’t it possible since Python 3.9?

    • @anthonyraf
      @anthonyraf 4 месяца назад +5

      I'm wondering why did he mentionned python 3.12

    • @Indently
      @Indently  4 месяца назад +34

      As RUclipsrs 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.

    • @anthonyraf
      @anthonyraf 4 месяца назад +4

      @@Indently Thanks for the precision : )

    • @thomaskobberpanum1311
      @thomaskobberpanum1311 4 месяца назад +13

      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.

    • @Indently
      @Indently  4 месяца назад +12

      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.

  • @Smart_Coder7
    @Smart_Coder7 4 месяца назад +4

    What is the difference between asyncio, multithreading and multiprocessing. These things do the same thing, don't they?

    • @Slackow
      @Slackow 4 месяца назад +7

      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

    • @Smart_Coder7
      @Smart_Coder7 4 месяца назад +1

      ​@@SlackowThanks

    • @jacknguyen5220
      @jacknguyen5220 3 месяца назад +7

      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.

  • @ritankarbhattacharjee7661
    @ritankarbhattacharjee7661 4 месяца назад

    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?

    • @NoProblem76
      @NoProblem76 4 месяца назад +2

      stick to the "sync_to_async" and "async_to_sync" decorators, it has some event loop and thread pool management baked in.

  • @michaelhoffmann2891
    @michaelhoffmann2891 2 месяца назад

    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.

  • @Johannes
    @Johannes 3 месяца назад +1

    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.

    • @Indently
      @Indently  3 месяца назад +1

      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!

  • @abdulnafihkt4245
    @abdulnafihkt4245 4 месяца назад

    We can use celery for seperate thread task na? Sorry if my question is wrong.

    • @jacknguyen5220
      @jacknguyen5220 3 месяца назад

      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.

  • @senyai
    @senyai 4 месяца назад +2

    Naked `dict` type is not the best thing, please specify key/value types or let mypy infer the types.

  • @oliverli9630
    @oliverli9630 4 месяца назад +1

    coooooooooool

  • @mcdolla7965
    @mcdolla7965 3 месяца назад

    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.

  • @Smart_Coder7
    @Smart_Coder7 4 месяца назад +2

    @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. 😊

    • @flavio273
      @flavio273 4 месяца назад +5

      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

    • @Lanxxe
      @Lanxxe 4 месяца назад +5

      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.

    • @Smart_Coder7
      @Smart_Coder7 4 месяца назад +3

      ​@@LanxxeThanks

    • @mahdirasouli6005
      @mahdirasouli6005 4 месяца назад +3

      It's called type annotation

  • @ildarshagidullin5205
    @ildarshagidullin5205 12 дней назад

    I thought I need to update my pycharm again because of the top right corner icon

  • @vandelayindustries2971
    @vandelayindustries2971 4 месяца назад +2

    Why use asyncio.create_task in your main function and then await the result? Why not just await fetch_status directly?

    • @jacknguyen5220
      @jacknguyen5220 3 месяца назад +4

      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.

  • @iham1313
    @iham1313 3 месяца назад +2

    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)?

    • @Indently
      @Indently  3 месяца назад +5

      It's a personal preference, I hate using dot notation with types.

  • @sw-code6027
    @sw-code6027 4 месяца назад +1

    We can do it using threads so why asynchronous?

    • @cyrilemeka6987
      @cyrilemeka6987 4 месяца назад +2

      My question exactly!

    • @SkyyySi
      @SkyyySi 4 месяца назад +1

      This IS multi threading. It's just that this way, you can still use async/await as syntactic sugar.

    • @cyrilemeka6987
      @cyrilemeka6987 4 месяца назад

      @@SkyyySi ohh

    • @Indently
      @Indently  4 месяца назад +1

      It's important to note that AsyncIO is single threaded.

    • @sw-code6027
      @sw-code6027 4 месяца назад

      @@Indently Well this information is new for me. Thanks a lot

  • @hlubradio2318
    @hlubradio2318 Месяц назад

    More examples

  • @TechnoPlayer64
    @TechnoPlayer64 4 месяца назад

    First, pin❤

  • @gohilurvish
    @gohilurvish 3 месяца назад

    Somehow I prefer "threading"

  • @seppmaier4858
    @seppmaier4858 4 месяца назад +4

    @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?

    • @Indently
      @Indently  4 месяца назад +3

      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 :)

    • @uKaigo
      @uKaigo 4 месяца назад

      ​@@Indently i know that would probably be more complicated, but I think you should also type the generics like those dicts

    • @Indently
      @Indently  4 месяца назад +1

      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?

    • @uKaigo
      @uKaigo 4 месяца назад +2

      @@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
      @uKaigo 4 месяца назад

      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

  • @sorrefly
    @sorrefly 4 месяца назад

    Threads for lazy people