Это видео недоступно.
Сожалеем об этом.

Next-Level Concurrent Programming In Python With Asyncio

Поделиться
HTML-код
  • Опубликовано: 17 авг 2024
  • If your software interacts with external APIs, you need to know concurrent programming. I show you how it works in Python and then take it to the next level by showing advanced concurrent features such as using async with generators and comprehensions, show a simple way to turn blocking code into concurrent code, and then I cover how concurrency affects software design and architecture.
    The code I worked on in this episode is available here: github.com/Arj....
    💡 Get my FREE 7-step guide to help you consistently design great software: arjancodes.com....
    💻 ArjanCodes Blog: www.arjancodes...
    🎓 Courses:
    The Software Designer Mindset: www.arjancodes...
    The Software Designer Mindset Team Packages: www.arjancodes...
    The Software Architect Mindset: Pre-register now! www.arjancodes...
    Next Level Python: Become a Python Expert: www.arjancodes...
    The 30-Day Design Challenge: www.arjancodes...
    🛒 GEAR & RECOMMENDED BOOKS: kit.co/arjancodes.
    👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!
    💬 Discord: discord.arjan....
    🐦Twitter: / arjancodes
    🌍LinkedIn: / arjancodes
    🕵Facebook: / arjancodes
    👀 Code reviewers:
    - Yoriz
    - Ryan Laursen
    - James Dooley
    - Dale Hagglund
    🎥 Video edited by Mark Bacskai: / bacskaimark
    🔖 Chapters:
    0:00 Intro
    1:10 Concurrency vs parallelism
    2:33 The Global Interpreter Lock
    3:06 The benefits of concurrency
    4:36 Recap of asyncio in Python
    7:13 Using gather to send out multiple requests
    9:44 How async and await are integrated into Python's syntax
    11:54 Turn blocking code into concurrent code
    16:04 Async http requests
    16:53 Aiohttp
    17:50 Concurrency, design patterns, and architecture
    #arjancodes #softwaredesign #python
    DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!

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

  • @ArjanCodes
    @ArjanCodes  10 месяцев назад

    💡 Get my FREE 7-step guide to help you consistently design great software: arjancodes.com/designguide.

  • @MrTrebor2
    @MrTrebor2 2 года назад +185

    Let's go deeper in async. Exceptions handling, pubsub pattern and how to manage back pressure.

  • @arthurguz2672
    @arthurguz2672 2 года назад +68

    Regarding your criticism about nested contextmanagers (at 17:35)...
    Since Python3.10 you are able to write something like this:
    async with (
    aiohttp.ClientSession() as session,
    session.get(url) as response,
    ):
    return await response.json()
    This is super usefull :)

    • @jeroenvermunt3372
      @jeroenvermunt3372 2 года назад +12

      Thanks a lot for this! Why do I have not seen this before.
      Is it just me or is python so much getting so much cleaner as javascript for these types of thing (I'm a beginner at javascript so totally biased, I think it looks ridiculous)

    • @kosmonautofficial296
      @kosmonautofficial296 2 года назад +1

      woah cool!

    • @user-hn6cc2tg2j
      @user-hn6cc2tg2j Год назад +1

      Thanks a lot for this

    • @shivampundir1829
      @shivampundir1829 Год назад +2

      With python 3.11 we can now use Taskgroups as well.

    • @AhriaHasegawa
      @AhriaHasegawa Год назад +1

      What! This is so cool! Thank you for this!

  • @BladeAndrade
    @BladeAndrade 2 года назад +87

    I'd be great to see a series of concurrency/parallelization in python.

  • @lawrencedoliveiro9104
    @lawrencedoliveiro9104 2 года назад +29

    15:13 You could leave out both the “await” and the “async”. So “send‗async‗request” becomes a regular function which returns an awaitable.

  • @indraxios
    @indraxios 10 месяцев назад +3

    Best video on async, finally someone who shows why , when and how to use async. Cleared all my doubts, Thank you

  • @leoff2
    @leoff2 2 года назад +9

    you didn't mention the difference between using `requests` + `to_thread`, and `aiohttp`. Although `aiohttp` is more verbose, it is faster since it involves no threads. You can also reduce indentation by creating the session beforehand and reusing it.

  • @arturkabitcher
    @arturkabitcher 2 года назад +4

    Arjan, I really like your videos. Could you please make more content on async Python? This content (unlike many other topics) is barely covered on RUclips, so you'll be guaranteed to have lots of views and we'll be guaranteed to have a more deep understanding of that complex topic.
    Multiprocessing and multithreading topics are also highly anticipated.

  • @sval4020
    @sval4020 2 года назад +7

    Very helpful video, Arjan! Exactly what I was looking for! I will be looking forward to even more asyncio videos from you! Thank you!

    • @ArjanCodes
      @ArjanCodes  2 года назад +1

      Glad to hear it was helpful!

  • @CaptainCsaba
    @CaptainCsaba 2 года назад +1

    I can't count the amount of times I've tried to find the best method for asnyc requests. I've used aiohttp just like you but it never felt right, but I never understood asyncio enough so this was always something I was scared of more than I should have. I am so happy that you have actually shown the best way to do it, it will help a lot in my everyday work!

  • @simmonslucas
    @simmonslucas Год назад +8

    I would love more examples using queues and event loops

  • @BlackCloudsAC
    @BlackCloudsAC 2 года назад +9

    Hey Arjan, thanks for posting this! You've opened my eyes to asyncio.
    I was wondering if you could explain when you'd use async methods vs when you would use Multi Threading through a ThreadPool?
    Many thanks!

  • @julienstanguennec9563
    @julienstanguennec9563 Год назад +2

    Another video on parallel programming could be very nice ! Thanks for all the material you provide, it's really helpful.

    • @ArjanCodes
      @ArjanCodes  Год назад +1

      Thanks so much, Julien, glad the content is helpful!

  • @cetilly
    @cetilly 10 месяцев назад +1

    Love, love, love your presentation style; so systematic, well thought out, and articulate.

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

    This was really very helpful. Can't be not selfish to ask you for more of these. Thanks! 😌

  • @lawrencedoliveiro9104
    @lawrencedoliveiro9104 2 года назад

    12:20 By the way, forgetting the “await” in front of “asyncio.sleep()” is a mistake I have made a few times. How about you?

  • @draufunddran
    @draufunddran 2 года назад +1

    I would like to have more videos about concurrency and asynchronous stuff... Please don't wait too long for the video about this topic.

  • @fartzy
    @fartzy 2 года назад +1

    I think one way to say it - to say parallelism vs concurrency - is to say the system parallelizes. However, the system handles concurrency. The system chooses to parallelize. The system reacts to concurrency.

  • @tobilobaogunware6686
    @tobilobaogunware6686 2 года назад +1

    Hi Arjan, your videos have been of really great help to me. If possible, a video about using the Aiohttp module with asyncio would be really nice. Fingers crossed. Thank you for all you do!

  • @davidmoremad
    @davidmoremad 2 года назад

    What a coincidence... I was dealing with this packages (asyncio & aiohttp) and you published this video today, lol.... Now I understand some things I actually really need to improve performance.
    Only 2 things I have to say:
    - I've read aiohttp is faster than requests but readable.
    - Request performance drops quite a bit when cookies are used and I'm not sure why. (ClientSession(cookies=mycookies))
    👍🍺 Great video mate

  • @sugo8920
    @sugo8920 Год назад +1

    I'm in love with async python now. Almost all apps will use other services. Almost everything uses REST now. This greatly speeds up by code and it more fun than sync programming.
    I recommend integrating your applications with Datadog if you can with APM enabled. Very satisfying to see parallel traces and great for optimizations as it can be used to uncover things that one may have thought were running in parallel but were not.
    Last is, to_thread is not a complete substitute for using aiohttp. Threads will start up slighly slower and I noticed that only 8 threads were running at one time when I used, where as if I used aiohttp everything ran at the exact same time and I was was able to run 100s of request in parallel were as with to_thread I was limited. Perhaps there is some env var i could have set to prevent the 8 thread limit but I could not find any docs on the topic.

  • @PaulSebastianM
    @PaulSebastianM 2 года назад

    Concurrency and Parallelism are two steps towards multi-threading (green or not).
    Concurrency is the first step, and means being able to define and start more than one function at a time without waiting for a return value at the call site.
    The two functions can run in series or in parallel, on one shared thread or two separate threads, it all depends on the system and hardware and load. They also run in the same process that started them.
    Parallelism is the second step, it is Concurrency but with an explicit request to try and run truly in parallel, by explicitly using an operating system thread for each function call. But it actually does not imply that the functions will truly run at the same time. It's up to the system to schedule and run these threads, which could be in parallel or in series if there is too much load on the system.

  • @robertmielewczyk4219
    @robertmielewczyk4219 2 года назад +1

    It would be great to see a video about exception handling with asyncio, and forever running tasks using asyncio.

  • @ATXAdventure
    @ATXAdventure Год назад

    I always check out Arjan when I need to brush up on ways to accomplish things in python.

  • @Theeoldmann
    @Theeoldmann Год назад

    IDK how or why, but the subject matter of your videos seem to show up (RUclips suggestion page) exactly when I need a bit more understanding of what I'm actually working with at the time. The providence is well received and please keep making great videos. Thank you

  • @MrJjthecronfan
    @MrJjthecronfan Год назад

    Thanks so much - Learning a lot from here . A friend of mine recommended your channel , now I am passing it on to others . Good Luck

  • @kunvensoong6047
    @kunvensoong6047 2 года назад +2

    Great video! Looking forward for a video that refactors sync+unittest to async+unittest

    • @ArjanCodes
      @ArjanCodes  2 года назад

      Thank you, glad you liked it!

  • @demolazer
    @demolazer 6 месяцев назад

    Just what I needed. I've got an app making a bunch of one by one API and SSH calls to my server which takes forever right now.

    • @ArjanCodes
      @ArjanCodes  6 месяцев назад

      I'm glad the video was helpful!

  • @HIMixoid
    @HIMixoid 2 года назад +1

    Great! I hope more videos about asyncio are coming ^___^

  • @CharleswoodSpudzyofficial
    @CharleswoodSpudzyofficial Год назад

    Woah... I'm blown away. Another new level for my Python code career

    • @ArjanCodes
      @ArjanCodes  Год назад

      Thanks so much Charles, glad the content is helpful!

  • @sudadal-ajili6205
    @sudadal-ajili6205 2 года назад +1

    Hey Arjan! Thank you for such amazing contents I have learned a lot from your videos. It will be great to see videos about Threading and Multiprocessing.
    Thank you again.

  • @JohnMatthew1
    @JohnMatthew1 2 года назад +1

    As always, I always learn something from your videos, THANK YOU. The to_thread() is new to me, very cool
    Too much sun? Nice color.

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

    you provide really good examples in your tutorials. Amazing channel.

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

      Thank you so much! Glad you like the content.

  • @JordanBonnett
    @JordanBonnett Год назад

    All other videos and tutorials I saw show the same example with asyncio.sleep()... Thanks for create a custom example a explain very well this tricky subject of python asynchronous programming. +100

  • @sebastianzander87
    @sebastianzander87 5 месяцев назад

    Since you said that you "don't like these nested 'with' statements" 17:36, I remember there is a Python feature probably not so well known: you can declare multiple variables in one 'with' statement by delimiting them by commas 🙂

  • @71sephiroth
    @71sephiroth 8 месяцев назад

    Pfff... nobody knows the struggle of getting Mewtwo on the first try. ;) Thanks for the comprehensive introduction!

  • @experimentalhypothesis1137
    @experimentalhypothesis1137 11 месяцев назад

    more async videos please, lets go deeper with u - u can explain this stuff very well

  • @seeds_of_growth-yi5gx
    @seeds_of_growth-yi5gx Год назад

    I really like the practical approach of exploring the topic, it's my favorite way of learning things. Keep going!
    just subscribed and signed up for the diagnosis course, eager to start and jump to the next ones. Thanks for sharing

  • @k98killer
    @k98killer 2 года назад +1

    Comment before watching the full video: I wrote my first bit of code using asyncio (an example implementation of a library I've built) just a few days ago after watching your previous videos on asyncio. Very cool. Building an interactive CLI tool that runs a networking application in the background has been on my to-do list for a long time, so I'm quite happy I figured it out.
    On a separate topic, have you considered making a video on publishing python packages to PyPI?

  • @re.liable
    @re.liable 2 года назад

    That type union is dope :O
    Haven't coded in Python for a while; been trying to get into JS frameworks. I love that union type :D

  • @aashayamballi
    @aashayamballi 2 года назад

    Loving it! Thanks Arjan.. this video was very informative, would like to see more videos on async python 🙏

    • @ArjanCodes
      @ArjanCodes  2 года назад +1

      Thank you, glad you liked it! I’ll surely revisit async in the future.

  • @AMMullan
    @AMMullan 10 месяцев назад

    Amazing video and so well explained! I’ve been trying to get a better understanding of how to use asyncio and it’s not easy but you’ve done an incredible job. Will definitely be checking out your other videos. Thanks man!!!

    • @ArjanCodes
      @ArjanCodes  10 месяцев назад

      I'm really glad it was helpful! Welcome to the channel :)

  • @TheEmrobe
    @TheEmrobe 2 года назад

    Awesome tutorial and all, but what really blew my mind was the existence of a Pokemon-API :D

    • @ArjanCodes
      @ArjanCodes  2 года назад

      I know! After discovering that, my life is complete.

  • @eldarmammadov7872
    @eldarmammadov7872 Год назад

    great explanation with giving right examples

  • @michtesar
    @michtesar 2 года назад

    That is very useful and interesting video discussing asyncio library. I was about to read the docs, but now I think I don't have to, thanks to your video! Great content! Thanks for that!

  •  2 года назад +6

    Did not know asyncio.to_thread, have been using loop.run_in_executor, which requires more setup.
    Also, reusing the session will save you quite a bit of time when sending many requests to the same end point, as it will reuse the connection.
    At work, saving the requests.Session halved the runtime of the application, which was was enough and save us the port to asyncio...

    • @leoff2
      @leoff2 2 года назад +1

      to_thread is new from Python 3.8 on, if I'm not mistaken

    • @user-et4hx1vb2o
      @user-et4hx1vb2o 2 года назад

      First: Great video as always, Arjan! I would really love to see some advandced asyncio topics on your channel.
      Some additional thoughts on Álvaro's post and the video:
      1. Using a session with aiohttp or e.g. httpx brings the same benefits - especially if you have things like authentication in your requests, too.
      2. Using asyncio.to_thread has two downsides: First you have the overhead for creating a thread, which consumes resources. Second Python thread execution is handeled by the OS, so you effectivly have no real control, when execution of the thread is interrupted and continued. This takes away some of the control over the program flow from the programmer - while using native coroutines are a bit more predictable. Apart from fast concurrent programs this is something I like most about asyncio - having so much control. So i personally would always prefer a native async library over using a synchronous one with asyncio.to_thread or loop.run_in_executor. If there's no quality alernative for a synchronous library, using asyncio.to_thread a great way to make you code asynchronous, absolutely agreed.
      3. If you don't like deep nested context managers, there are some alternatives: not sure for aiohttp but for httpx you can use:
      client = httpx.AsyncClient()
      await client.aclose()
      With a httpx you also have only have one contextmanager (not two) using a session this way:
      async with httpx.AsyncClient() as client:
      r = await client.get('www.example.com/')
      As context managers are very common in async libraries (e.g. if you use sqlalchemy) you could also an AsyncExitStack to enter many contexts without having to indent every time.
      4. You meantioned async Python getting easier with Python 3.10. Was there something added in this version for asyncio? I really can't remember - or did you mean it more generally with the features added in versions before?
      I hope this post does not sound to negative. I really love your videos and I'm very thankfull for learning so much from them! Just wanted to point out some things to consider.
      One last question:
      Are there are payment options aparart from credit card on your homepage or would you consider to add some other options in the future?

    • @the1GeniX
      @the1GeniX 2 года назад

      Thanks for this!

    • @kosmonautofficial296
      @kosmonautofficial296 2 года назад

      Try out concurrent.futures.ThreadPoolExecutor() it may be even easier. The downside to using .to_thread is that it is using threads to get concurrency and passing the operation to the OS rather than handling it manually. The benefit of asyncio is that you have concurrency in a single thread and you are controlling it but to do that you need to await a function that supports async operations. For that you would need to use aiohttp or httpx instead of requests which relies on the socket library which is blocking and doesn't support async.

  • @NostraDavid2
    @NostraDavid2 2 года назад

    I hope one of your future videos goes into what the async keyword actually does with your code.
    Sure, I know that *now*, as I worked with async before out of necessity (which was a great learner for me), but before that I was completely befuddled on what that keyword does and why it made my code stop running unless I used something like async.gather().

  • @Whatthetrash
    @Whatthetrash 2 года назад

    Thanks so much! Love to see your videos. Thanks so much for making and sharing them! ^_^

  • @getpoked101
    @getpoked101 2 года назад

    Great video. We are doing a bunch of data ingestion from a rest api right now so this is very top of the mind.

  • @anandptyagi5275
    @anandptyagi5275 Год назад

    Hello, Thank you for the information. I like your way of teaching, and I would like to see more advanced topics in python explained by you, If possible please do. Have a good day, thanks.

  • @guplex
    @guplex 2 года назад

    Thanks! Would love more asyncio content

  • @walis85300
    @walis85300 2 года назад

    Thanks for improving the way the video looks

  • @user-mi2bb8bm6s
    @user-mi2bb8bm6s 11 месяцев назад

    Thank you for very helpful video.
    Mewtwo is one of legendary poketmon, but you got it with just two choices!

  • @mentefria98
    @mentefria98 Год назад +1

    this was so hepful, thanks.

    • @ArjanCodes
      @ArjanCodes  Год назад +1

      Thanks so much, glad the content is helpful!

  • @EricNeilson_
    @EricNeilson_ Год назад

    When you introduce aiohttp and describe the differences, this is hilarious. For a relatively noob like me, it's a lot to digest :) But it's great.

  • @nateriver8261
    @nateriver8261 2 года назад

    Very helpful video, Arjan!

    • @ArjanCodes
      @ArjanCodes  2 года назад

      Thanks Nate, glad it was helpful!

  • @Krzako
    @Krzako Год назад

    Just one thing. You compare sync version with print in loop vs async with just one print. Of course it doesn't change the fact that async is much faster but to get more accurate results we have to remember to measure functions without unwanted code like slow prints ;-)
    Anyway great video. I'm mid developer that switched to python from other scripting language. I recently found your channel and liked it very much. I'm learning a lot from you. Thank you for all your great work!

  • @jorgev4656
    @jorgev4656 2 года назад +1

    hi. whats the difference between multiprocesing, and asyncio ?

  • @drvdrv-ct8qt
    @drvdrv-ct8qt 13 дней назад

    using asyncio with "to_thread" function is like using electric car to save environment and charge it with diesel generator at home

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

    From what I read, that short answer describing a difference between concurrency and parallelism is incorrect. Parallelism is described correctly, but concurrency is either two lines of customers ordering from a single cashier taking turns, or each line having its own cashier.

  • @VictorNedilko
    @VictorNedilko Год назад

    Great tutorial and fantastic way of presenting.
    Thanks

  • @riptorforever2
    @riptorforever2 Год назад

    Great lesson. Thanks

    • @ArjanCodes
      @ArjanCodes  Год назад

      Thank you, glad you liked the video!

  • @EekaMau5
    @EekaMau5 Год назад

    Thank you ! this tutorial was awesome !

  • @huonglarne
    @huonglarne Год назад

    this is incredibly helpful. Thank you so much

    • @ArjanCodes
      @ArjanCodes  Год назад

      Thanks so much, Mo Gung, glad the content is helpful!

  • @dmytrokorbanytskyi1586
    @dmytrokorbanytskyi1586 2 года назад

    Thanks for the video! I really like your coding style )

    • @ArjanCodes
      @ArjanCodes  2 года назад

      Thank you - glad you like the video!

  • @tharo20
    @tharo20 Год назад

    This video helped me a lot! thank you so much!💌

  • @Obi-Wan-Kenobii
    @Obi-Wan-Kenobii 2 года назад

    Thanks for covering async.. I have once written an aiohttp based webservice to retrieve data from neo4j.. Aiohttp github page have some nice async libraries collections.. I hope more developers will release async libraries as well..

  • @blessedday6808
    @blessedday6808 2 года назад

    1:47 gives a real life matephor of how asyncio computation compares to parallel computation
    3:57 explains what async def means
    I greatly appreciate that you used pokemon in this video. It really helps me to see with clarity the difference between the actual subject matter.
    Also 10:28 Rage will most likely yield a Cubone or Gyrados!
    This video makes me want to sharpen up my list comprehension skills. Thank you for explaining the difference between parallel and concurrent code execution. That really made a lot of sense. Especially bringing up the fact that computers use different amounts of cores in a cpu chip. I didn't know that python can manipulate HOW our chips process our code. I learned something new here.

  • @valentinasinichenko751
    @valentinasinichenko751 Год назад

    this has been extremely helpful

  • @mabdurrafeyahmed9256
    @mabdurrafeyahmed9256 Год назад

    Thanks for telling concurrency vs parallelism. I will be confident in my next interview

  • @LeThorquato
    @LeThorquato 2 года назад

    I would love to see someone try to make a video with a full solution, like asyncio getting http requests like thousands of them, and loading on a database, it would use pandas? or just dictionaries or data classes?

  • @thghtfl
    @thghtfl 10 месяцев назад

    Thank you so much, man! Brilliant explanation!

    • @ArjanCodes
      @ArjanCodes  10 месяцев назад

      I'm glad it helped!

  • @sumaryze
    @sumaryze Год назад

    Thanks for your really useful video. Can you do another video on specifying which part of the program to run concurrently and sequentially?

  • @lawrencedoliveiro9104
    @lawrencedoliveiro9104 2 года назад

    4:13 “await” is actually an expression, not (necessarily) a statement.

  • @MrEloska
    @MrEloska 2 года назад

    Could you make some advanced video on GIL? Where you can explain how it works, how to "bypass" it etc?

  • @kevon217
    @kevon217 Год назад

    I’m just starting to get into async. Have you thought about doing a tutorial on async use in the context of LLM calls and frameworks?

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

    To get a more accurate difference in sync and async calls, You should have tested without printing the Pokemon for each of the loop iteration.
    Since Print statement in itself takes some time.

  • @Brainsucker92
    @Brainsucker92 15 дней назад

    Kind of mind boggling. I studied software engineering at an university in Germany for several years and they never bothered to teach us such moderns way of software engineering. I'm learning way more about software engineering on this channel than at the university, which is both fascinating and disturbing at once. I'm kind of disappointed by the things I learned at the university. The stuff I learned there barely scratched the surface of actual software engineering.

  • @holographicsol2747
    @holographicsol2747 Год назад

    really helpful thanks man

  • @therealslimaddy
    @therealslimaddy 2 года назад

    Very useful, thanks 🙏🏼

  • @xlerb_again_to_music7908
    @xlerb_again_to_music7908 2 года назад

    This was just complex enough that I thought - We need a diagram. The verbage is rather fast alas when trying to grasp new ideas, plus watch what is going on with the typing. Diagrams pls!

  • @gamingland2074
    @gamingland2074 2 года назад

    this is how i kill boredom , watching something usefull

    • @ArjanCodes
      @ArjanCodes  2 года назад +1

      Thanks so much, glad it was helpful!

  • @LittleCrazy2life
    @LittleCrazy2life 2 года назад +2

    if possible make a video on writing test cases on async functions!

    • @ArjanCodes
      @ArjanCodes  2 года назад +3

      Thanks for the suggestion!

  • @zacharythatcher7328
    @zacharythatcher7328 2 года назад +2

    I am pretty certain that using to_thread defeats the entire purpose of using async instead of threading. It means that you’ll pop off a 8mb RAM thread specifically for that task, which is exactly what you are typically trying to avoid with async. You could threaded those tasks and gotten the same result with no memory usage improvement.
    The real benefit of async in python is how much RAM you save and the ease of memory management. You can open WAY more concurrent async requests than threaded requests if they return objects smaller than 8mb. And if they returned objects larger than 8mb each then you wouldn’t run into any stack overflow issues (hopefully python has a way of handling this situation automatically regardless).

    • @macewindont9922
      @macewindont9922 2 года назад

      I think you're right. There has to be a coroutine somewhere for the magic of async to happen. The yield keyword lets python know when to release control to another function. How would to_thread let python know where to yield within an otherwise syncronous function?

    • @ArjanCodes
      @ArjanCodes  2 года назад +1

      The goal of asynchronous programming is not to save RAM (even though that might be a benefit with regards to using threads). The goal is to offer an easy syntax for performing tasks concurrently. to_thread is simply a fix for the case where you have a synchronous function that you’d like to run concurrently. If you want to open tons of concurrent requests and you’re worried about RAM usage, don’t use to_thread because that’s not what it for. Use a library like aiohttp that is already async.

    • @user-et4hx1vb2o
      @user-et4hx1vb2o 2 года назад

      @@macewindont9922
      asyncio.to_thread creates a coroutine which "yields" the result of the thread created. This coroutine is awaitable. The thread on the other hand is executed by the OS which starts, interrupts and stops the thread with it's own logic.

    • @comfuse
      @comfuse 2 года назад +1

      @@ArjanCodes "The goal is to offer an easy syntax for performing tasks concurrently". But only in a cooperative manner. This is in stark contrast with preemptive multitasking (e.g. threads). Cooperative multitasking has many benefits (and downsides) compared to preemptive multitasking, one of which was mentioned by OP. By introducing to_thread, you risk nullifying them.

    • @lawrencedoliveiro9104
      @lawrencedoliveiro9104 2 года назад

      @@comfuse So feel free to offer an alternative.

  • @djl3009
    @djl3009 2 года назад

    Nice one. Thanks!

  • @tiktuktiktuk
    @tiktuktiktuk 2 года назад

    Would like to see more on async..

  • @jochinq3116
    @jochinq3116 Год назад

    Awesome, thanks for this video! maybe you would like to make a video about asyncio and websocket?🤩

    • @ArjanCodes
      @ArjanCodes  Год назад

      Thank you, for suggestion, I have put it in my list.

  • @pypypy4228
    @pypypy4228 11 месяцев назад

    7:40 Could you please explain why would we want this async syntax for doing the task in a pretty much synchronous way?

  • @goodnewsjohn2482
    @goodnewsjohn2482 2 года назад

    really nice video. please can you do something with celery and gevent

  • @mahdiasadnejad3804
    @mahdiasadnejad3804 2 года назад

    Thank you, it was great . let's go deeper!

  • @FEchtyy
    @FEchtyy Год назад

    so helpfull! thanks a lot

  • @vigneshmurali1994
    @vigneshmurali1994 2 года назад

    What is the shortcut you are using to get into insert mode ( Block Cursor). It would be helpful if you could do a VS Code quicktour

  • @MujtabaMuhammadKhushtaba
    @MujtabaMuhammadKhushtaba 2 года назад

    awesome video, looking forward to new videos

  • @munteanionut3993
    @munteanionut3993 2 года назад +3

    11:08 - "..., but it still run these things in order". If it is like this, then why use the "async" keyword in the for-loop? Does it have any effect then? I presume a syntax error would be thrown if we didn't use it. But if it doesn't have any effect, then why use it?

    • @yparwani
      @yparwani 2 года назад +1

      I also wished that this was explained more - I think it has to do with the different calls being added to the "event loop" which gather does automatically.

    • @ArjanCodes
      @ArjanCodes  2 года назад +1

      It can still be useful to do this, for example if your loop goes through the steps of a pipeline, and the next step depends on the outcome of the previous one. By making the individual steps async, you can use the await syntax within the step itself and benefit from concurrent programming there.

    • @munteanionut3993
      @munteanionut3993 2 года назад

      @@ArjanCodes thank you!

    • @lawrencedoliveiro9104
      @lawrencedoliveiro9104 2 года назад

      async-for needs the additional ‗‗aiter‗‗ mechanism in order to work, ‗‗await‗‗ on its own isn’t enough.
      I haven’t found async-for to be all that useful. So far, anyway.

  • @marklandgraf7667
    @marklandgraf7667 Год назад

    "I HATE THOSE THINGS!" 3:26
    Me, too!

  • @omidasadi2264
    @omidasadi2264 Год назад

    Thanks for sharing it!
    But a ?
    I didn't get the limitation of GIL for using async

  • @rikschaaf
    @rikschaaf 2 года назад

    15:27 This looks a lot like java's fork/join. The create_task is like the fork, the await is like the join

  • @burakcelik3752
    @burakcelik3752 2 года назад

    TL;DR # Excellent, thank you for the great content.#
    I really appreciate your work as thinking how much time you spent to create high quality contents like this. I watched a lot of videos when I started coding but %90 of them was like demonstrating an isolated feature of Python using a Jupyter Notebook. I accept that I learned so much things from them but here was the problem. Even if I learned so much deep, for instance using generators as coroutines that is what basically what asyncio implements now, but I'd never managed to get that advanced techniques together I later realize that was because I learned all the things in isolation and would stuck when it comes to use them all together, that is, the links were missing. Arjan, you did a great job starting to talk about design patterns that I realized the missing links were not the techniques but the philosophy. Therefore I can't recommend your channel enough for people who stuck in the intermediate level of coding and don't know why can't take a step further regardless of whatever new things they learn. I know there must full of people in that situation and think a great deal of them gives up in the end. Your channel is just a treasure for the people in subject and must be spread. As for concurrency, I've always found using preemptive multitasking far from being elegant because it inherently is a brute force approach and I can't imagine anybody who don't love asyncio s/he learns the philosophy behind it. Since we don't have too many options for cpu bound problems because of GIL even if we tackle the problem by splitting it independent sub methods, we can do much for IO bound problems using asyncio and I would appreciate if you consider to continue on the subject. For instance, how can we develop our own cooperative multitasked classes aside from using the external ones (except sending the couroutines to threads). For instance, think a project that used requests-http for its internal processes but doesn't explicitly support asyncio for its public methods. It's obvious that it has capacity for doing that and we can branch and give it our own asyncio support with a little effort and it would be great to learn how an async class should be designed philosophically. The last word is thank you for your great channel and efforts again.

  • @kahvelikod
    @kahvelikod 2 года назад

    Very useful , Thank you.

  • @cristeapaul5634
    @cristeapaul5634 2 года назад

    Thank you so much for the video it was very helpful for me!
    I have one question regarding that handy JSON variables.
    Where should I keep some kind of typing variable in a bigger project?