• taaz@biglemmowski.win
    link
    fedilink
    English
    arrow-up
    0
    ·
    edit-2
    30 days ago

    So, the word here is parallelism. It’s not something specific to python, asyncio in python is just the implementation of asynchronous execution allowing for parallelism.

    Imagine a pizza restaurant that has one cook. This is your typical non-async, non-threading python script - single-threaded.
    The cook checks for new orders, pickups the first one and starts making the pizza one instruction at the time - fetching the doug, waiting for the ham slicer to finish slicing, … eventually putting the unbaked pizza into oven and sitting there waiting for the pizza to bake.
    The cook is rather inefficient here, instead of waiting for the ham slicer and oven to finish it’s job he could be picking up new orders, starting new pizzas and fetching/making other different ingredients.

    This is where asynchronicity comes in as a solution, the cook is your single-thread and the machines would be mechanisms that have to be started but don’t have to be waited on - these are usually various sockets, file buffers (notice these are what your OS can handle for you on the side, asyncIO ).
    With proper asynchronicity your cook can now handle a lot more pizza orders, simply because their time is not spent so much on waiting.
    Making a single pizza is not faster but in-total the cook can handle making more of them in the same time, this is the important bit, more in the closing note.


    Coming back to why a async REPL is useful comes simply to how python implements async - with special (“colored”) functions:

    async def prepare_and_bake(pizza):
      await oven.is_empty()  # await - a context switch can occur and python will check if other asynchronous tasks can be continued/finalized
      # so instead of blocking here, waiting for the oven to be empty the cook looks for other tasks to be done
      await oven.bake(pizza)  
      ...
    

    The function prepare_and_bake() is asynchronous function (async def) which makes it special, I would have to dive into asynchronous loops here to fully explain why async REPL is useful but in short, you can’t call async functions directly to execute them - you have to schedule the func.
    Async REPL is here to help with that, allowing you to do await prepare_and_bake() directly, in the REPL.


    And to give you an example where async does not help, you can’t speed up cutting up onions with a knife, or grating cheese.
    Now, if every ordered pizza required a lot of cheese you might want to employ a secondary cook to preemptively do these tasks (and “buffer” the processed ingredients in a bowl so that your primary cook does not have to always wait for the other cook to start and finish).
    This is called concurrency, multiple tasks that require direct work and can’t be relegated to a machine (OS) are done at the same time.
    In a real example if something requires a lot of computation (calculating something - like getting nth fibonnaci number, applying a function to list with a lot of entries, …) you would want to employ secondary threads or processes so that your main thread does not get blocked.

    Hopefully this was somewhat understandable explanation haha. Here is some recommended reading https://realpython.com/async-io-python/

    • grue@lemmy.world
      link
      fedilink
      English
      arrow-up
      0
      ·
      30 days ago

      Final EDIT: Reading it myself few times, a pizza bakery example is not optimal

      Yeah, I kept getting hung up on the notion of toppings being sliced or grated to-order, rather than bought from Sysco pre-prepped in big plastic bags like any normal pizzaria would do. Your analogy be fancy!