Crust of Rust: async/await

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ม.ค. 2025

ความคิดเห็น • 183

  • @yankee-in-london
    @yankee-in-london 3 ปีที่แล้ว +187

    I often feel I don't have enough time to watch a 30 minute video but spending 2+ hours watching your content is something I will make time for. Fantastic contribution to grokking Rust ... thank you.

  • @2Jujumba
    @2Jujumba 7 หลายเดือนก่อน +2

    Thanks!

  • @maxwellborden
    @maxwellborden 3 ปีที่แล้ว +33

    This was great! Even though you described it before, I didn't _get_ the "await/yield returns back to the thing that awaited" part until you answered the "do futures stay on a thread" question and then it clicked. And the async trait fn part was great because it always seems more intuitive when you describe why things that I expect should work, don't work. It also tied off why the stack variables part was important so nicely.

  • @nimaimpatel
    @nimaimpatel ปีที่แล้ว +3

    I finally understand the formal difference between concurrency and parallelism because of the 01:19:00
    Your content is amazing. Thank you for putting it out!

  • @echosystemd
    @echosystemd 3 ปีที่แล้ว +89

    Wow, this introduction is spectacular. Thanks for the beautiful content.

  • @Adam-cn5ib
    @Adam-cn5ib 3 ปีที่แล้ว +8

    This channel is the only one of its kind. Please don't ever stop! :)

  • @dinckelman
    @dinckelman 2 ปีที่แล้ว +13

    I'm not even a Rust engineer, and i've still sat through most of the video. This is the kind of content I love watching.

  • @deformercr6680
    @deformercr6680 10 หลายเดือนก่อน +1

    This was an amazing watch! The state machine unveil was an "aha" lightbulb moment for me, since I knew some of the background of how async executors work, but didn't really know how the macros worked and how the desugaring happened.
    Also, now that async traits are a thing, and RPIT in traits work, it would be great to have a follow up video discussing the new way and what are the gotchas there.
    Thank you so much for putting out amazing educational material like this for free! I only started learning rust like a week ago and I feel like I understand stuff so much more than I would expect to because of deep videos like this!

    • @jonhoo
      @jonhoo  10 หลายเดือนก่อน +1

      You may find th-cam.com/video/CWiz_RtA1Hw/w-d-xo.htmlsi=GcR0lZnT3tKds9UO interesting too :)

    • @deformercr6680
      @deformercr6680 10 หลายเดือนก่อน

      @@jonhoo wow, thank you so much!

  • @WilliamBrumble
    @WilliamBrumble 2 ปีที่แล้ว +2

    Man, was really hard wrapping my head around this, until I found this video.
    Thanks for sharing.

  • @christopher8641
    @christopher8641 2 ปีที่แล้ว +4

    You are super good at explaining things! Been learning a lot watching this series. Thanks!

  • @pedro.guedes
    @pedro.guedes ปีที่แล้ว +1

    thanks... it's great when we can see through the magic and the world makes sense again! I watch these things for start to finish...

  • @Seacrest.
    @Seacrest. ปีที่แล้ว +1

    this is the best lecture about async Rust. Thank you

  • @stepan_romankov
    @stepan_romankov 11 หลายเดือนก่อน +3

    Thank you so much. I feel I finally getting into rust and start understanding why compiler complains on my code and how to fix it.

  • @lemonsh
    @lemonsh 3 ปีที่แล้ว +2

    i love you. it helped me SO MUCH! i'm gonna send this video to all my rust friends who struggled with this. awesome channel, i like that livestream idea.

  • @Tszyu01
    @Tszyu01 ปีที่แล้ว

    More of this is needed on TH-cam. Thank you for comparing to other languages like JavaScript promises, that helped me personally.

  • @mike2ykme
    @mike2ykme 3 ปีที่แล้ว +8

    Hey I’ve always enjoyed your streams but somehow never knew you were writing a book. I’m glad you mentioned it so I was able to preorder a copy!

  • @sau-da-de
    @sau-da-de 2 ปีที่แล้ว +5

    For me the easiest way to understand async was writing it in C.
    Of course that not everyone knows C syntax, but it really gives you a complete understanding of how simple async is and how you can leverage blocking syscalls

    • @cashpay50
      @cashpay50 ปีที่แล้ว +4

      are there any resources out there about this? I kinda want a guide to implement this, thanks in advance!

  • @hanskessock3941
    @hanskessock3941 ปีที่แล้ว

    Useful? It was HUGELY valuable, especially given that it's one giant recorded live stream - simply amazing. Cheers!

  • @apffer
    @apffer 11 หลายเดือนก่อน +1

    excelent explanations on the hows and whys of async. Thank you! 👏🏻👏🏻

  • @DiegoSilva-dv9uf
    @DiegoSilva-dv9uf 3 วันที่ผ่านมา

    Valeu!

  • @PiyushKumbhare10
    @PiyushKumbhare10 5 หลายเดือนก่อน

    Pretty new to Rust and don't really have any async/await requirements or anything, but I've always wondered how it worked under the hood. This was the perfect video for learning, thanks so much 😄

  • @douglaslawrie3449
    @douglaslawrie3449 ปีที่แล้ว

    Thanks for the effort you put into these videos and trying to help people understand difficult stuff. Great teaching, great presentation, great video :)

  • @tiktak7004
    @tiktak7004 3 ปีที่แล้ว +1

    Thank you, Jon, I love videos that convey the essence professionally.

  • @qm3ster
    @qm3ster 3 ปีที่แล้ว +1

    At 1:25:20, one of the `&mut connections` is in the "futures" section and one in the "matched handler block", where all other "futures" have already been dropped, so there wouldn't be any problem with the compiler.

  • @IVIanSour
    @IVIanSour 2 ปีที่แล้ว

    I don't know any rust but I still watch these videos, idk why.. you're good

  • @simonsellar8482
    @simonsellar8482 3 ปีที่แล้ว +2

    Great content and delivery as always! Enjoying the new book too, interesting and challenging!

  • @wesleychaffin4029
    @wesleychaffin4029 ปีที่แล้ว

    Manually writing async FSMs in C really gives you appreciation for the niceties of this syntax sugar

  • @furkanturkal8414
    @furkanturkal8414 3 ปีที่แล้ว +1

    This... is such a great introduction. Thanks for sharing with us.

  • @flwi
    @flwi 10 หลายเดือนก่อน

    That was very useful. Coming from scala I now have a better understanding and know what the differences are.

  • @leoxiaoyanqu
    @leoxiaoyanqu 3 ปีที่แล้ว

    Just noticed it's your birthday today.. (what are the odds😅) Happy Birthday Jon! Thanks a lot for your high-quality videos! Super helpful!

  • @detoxifiedplant
    @detoxifiedplant 2 หลายเดือนก่อน

    excellent content as usual jon 👏

  • @ezengondolkozom3700
    @ezengondolkozom3700 2 ปีที่แล้ว +1

    2:12:23 why does it have to be Arc if it's executing in a single thread? Also why does it have to be a mutex if since it's a single thread then no concurrent access will be happening?

    • @peter9477
      @peter9477 2 ปีที่แล้ว +1

      The spawn() call requires it since it is allowed to handle it in a new thread, not only the current one. (It's not necessarily single threaded.)

  • @diwakargupta0
    @diwakargupta0 2 ปีที่แล้ว

    I am grateful to find this video. Thanks for awesome explanation :)

  • @YoloMonstaaa
    @YoloMonstaaa 3 ปีที่แล้ว +2

    Thank you, I'm very excited to watch this. :)

  • @user-ov5nd1fb7s
    @user-ov5nd1fb7s 3 ปีที่แล้ว +2

    A good way of picturing it is the number of awaits in your future equal to the number of states in the state machine it's being turned into. 15:00

    • @comradepeter87
      @comradepeter87 3 ปีที่แล้ว +3

      Yes that's how it works. Each .await encapsulates a state.
      async fn foo() {
      //State1 begins..
      let x = bar().await;
      //State1 ends ..
      //State2 begins..
      let y = x.prop1.setColor("whatever");
      let z = y.paint_screen().await;
      //State2 ends..
      // State3 begins ..
      return z.status_code;
      //State3 ends..
      //End state begins (shouldn't be poll()ed after this).
      }

  • @andredasilva6807
    @andredasilva6807 2 ปีที่แล้ว

    thanks a lot for this stream / vod. its amazing to see into detail, what a feature actually does. i hope we can see some more in the future (pun intended :-))

  • @jonarmani8654
    @jonarmani8654 2 ปีที่แล้ว +5

    0:25 "[This] is gonna be one of the shorter streams..." 2.5hrs long? Surprisingly correct.

  • @hermannpaschulke1583
    @hermannpaschulke1583 3 ปีที่แล้ว

    Thank you soooo much for this. I Finally understand how this stuff works.

  • @GamesMaxed
    @GamesMaxed 3 ปีที่แล้ว +4

    In the "Stack variables in async" example, how does chunk2 access x?

    • @comradepeter87
      @comradepeter87 3 ปีที่แล้ว +2

      Any variable that needs to be shared across states needs to be passed into the new state while transitioning.
      Example,
      // Inside poll() definition
      match *self {
      Self::State1(x) => {
      // Do stuff
      *self = Self::State2(x, some_new_state);
      // That's it for State1's job.
      },
      Self::State2(x, y) => {
      // ...
      *self = Self::End;
      },
      Self::End => {
      panic!("poll () shouldn't be called after future is completed");
      },
      _ => unreachable!(),
      }
      // End of match statement.
      Each time poll () is called, you do work and potentially shift into a different state.

  • @gritcrit4385
    @gritcrit4385 3 ปีที่แล้ว +1

    Awesome videos, man. I'm not getting enough free time to learn Rust, nor getting a chance to use it at work. But I hope someday it will happen.

  • @McCourtRC
    @McCourtRC ปีที่แล้ว

    This was incredibly helpful. Thanks!

  • @mzr4141
    @mzr4141 3 ปีที่แล้ว +2

    Would it make sense to have something like off-CPU analysis with bpf for futures on tokio executor? Some async-aware instrumentation might be able to hook into yield points and probe stack traces also saving timestamps.

    • @jonhoo
      @jonhoo  3 ปีที่แล้ว +1

      Yup, I think there's been some talk about that already. Possibly by having a tracing subscriber that introduces labels that you can then refer to from bpf too. It's a tooling ecosystem that's very much still in its infancy though!

  • @parthikpatel6108
    @parthikpatel6108 3 ปีที่แล้ว

    At 57:00, it's mentioned that if there's a future that's blocking on something that takes forever, (and therefore never yields), nothing ever gets the chance to run. But stuff can still run on another thread right? (Assuming the chosen tokio runtime is multithreaded?

    • @jonhoo
      @jonhoo  3 ปีที่แล้ว +1

      Yep, I talk about that a little later in the video. The devil is in the details here though. For example, Tokio implements (at least, implemented) an optimization that makes it faster to schedule a future that recently ran, which in turn makes that future not possible to steal. So if you block an executor thread, you _also_ block that additional future. Which may in turn prevent other futures from running that depend on that future running. Basically, blocking the executor can have grave and unexpected consequences, so don't do it 😅

    • @parthikpatel6108
      @parthikpatel6108 3 ปีที่แล้ว +1

      @@jonhoo noted. So what I'm gathering so far is that it's fairly easy to reap the full benefits of async/await as long as you adhere to a few simple rules

  • @OlivierEble
    @OlivierEble 3 ปีที่แล้ว +1

    This is so awesome. I finally know how to handle the issues that I have met with async in Rust.

  • @kiffeeify
    @kiffeeify 3 ปีที่แล้ว

    @32:52 "every await is an opportunity to chose to do something else". I assume this is only true if the awaited future is not immediately ready (im thinking of `std::future::ready()`) ?! I.e. futures are eagerly progressed, if possible. Only if there are some special os or async-framework specific calls controll will be yielded and other futures may progress. Is that correct?

    • @jonhoo
      @jonhoo  3 ปีที่แล้ว +2

      No, futures are *not* eagerly professed. The executor is allowed to to run the same future again, but is under no obligation to do so. And generally it'll take the opportunity to run another future instead.

    • @kiffeeify
      @kiffeeify 3 ปีที่แล้ว

      @@jonhoo thanks :)

  • @matheusbaptistella1880
    @matheusbaptistella1880 ปีที่แล้ว

    Thanks for the video, really helpful!

  • @jRsqILVOY
    @jRsqILVOY 3 ปีที่แล้ว +1

    Is there a reason Tokio itself reimplements the std::fs functions directly for example? Rather than having an executor-agnostic async standard library crate?

    • @jonhoo
      @jonhoo  3 ปีที่แล้ว +1

      Because in general the I/O resource types (like TcpStream) need to communicate with the executor in order for the executor to know what events to wait for when it decides to put a thread to sleep. And there is no executor-agnostic way to do that currently. We may eventually get something like that, but likely not any time soon.

  • @jauhararifin10
    @jauhararifin10 ปีที่แล้ว

    If my async function does an IO in a very deep call stack, does it have a lot of overhead everytime the future is polled? Because the poll need to check the state of the top of the call stack, down to the last call stack that actually perform the IO.

  • @budiardjo6610
    @budiardjo6610 ปีที่แล้ว

    really love your book, i am learn a lot.

  • @karelhrkal8753
    @karelhrkal8753 2 ปีที่แล้ว

    2:13:00 I feel there needs to be a way to get a warning when calling a blocking function in an async function. It would solve exactly these kinds of problems. I will have to look into it.

  • @forouharp9683
    @forouharp9683 3 ปีที่แล้ว

    Thank you alot for these streams, this one realy helped me in learning async 👍👍👍🙏🙏🙏❤❤

  • @irlshrek
    @irlshrek 2 ปีที่แล้ว +1

    It's funny, coming from JavaScript, conceptualizing futures and async await feels natural. It's threads that confuse me yet it looks like most people in the comment section are in the opposite camp

  • @ntippy
    @ntippy ปีที่แล้ว

    5 stars, well done.

  • @matts1839
    @matts1839 ปีที่แล้ว

    Good stuff for hardcore C++ coders other than just dummy hello worlds.. Big up!

  • @mnn3392
    @mnn3392 3 ปีที่แล้ว +1

    Does anyone knows if there is any programming language that supports execution of async with multiple threads like rust?

  • @stretch8390
    @stretch8390 5 หลายเดือนก่อน +1

    Is anyone able to tell me whether anything has changed with async/await in rust since this video? Just wanting to confirm before committing the time

    • @josephsmith5110
      @josephsmith5110 5 หลายเดือนก่อน +2

      async fn in traits is now allowed and RFCs for async closures and naming the return type of (async) methods have been accepted.

  • @amirhossein4684
    @amirhossein4684 2 ปีที่แล้ว

    Greate explanation, In Asynchronous mutexes you said that using standard library mutex for short operation is safe but I think it's true for single thread async app, But if we have more than 1 thread always there is a chance that two parts of code refer to same mutex that may lead to deadlock, certainly in short operation this chance is very low but it's still possible.

  • @zoulock
    @zoulock 3 ปีที่แล้ว +1

    Perfect timing!

  • @bozhidaratanasov7800
    @bozhidaratanasov7800 ปีที่แล้ว

    I know it's a big topic, but I want to dive deeper in how unix based systems actually handle async events and parallel operations. At the very least I want to understand what the basic level async facilities are (network calls, file operations, what else could be async??). Do you have recommendations for materials on this topic?

  • @leetom5192
    @leetom5192 3 ปีที่แล้ว

    This is very useful even though I don't use rust, only c++. This will definitely help for when I make my own multithreaded async yield system using c++20 co routine features. (With c++20 modules features, too. Let's see how many compiler bugs I will hit!)

  • @fgtdjkg
    @fgtdjkg ปีที่แล้ว

    brilliant explanations

  • @kvadratbitter
    @kvadratbitter ปีที่แล้ว

    How relevant/up to date is the content in this vid? Has async in Rust changed a lot in 2 years?

    • @jonhoo
      @jonhoo  ปีที่แล้ว +1

      Should still be pretty much up to date!

  • @cloudsquall88
    @cloudsquall88 2 ปีที่แล้ว

    What an amazing video!

  • @milanrastelli4026
    @milanrastelli4026 3 ปีที่แล้ว

    Wow it’s the missing guide on async/wait I’ve been looking for. Watch the full video in one go, every second of it worth it.

  • @arnetriesyoutube
    @arnetriesyoutube ปีที่แล้ว

    This was amazing.

  • @deanroddey2881
    @deanroddey2881 3 ปีที่แล้ว +1

    Any reasonable threaded scenario would use a thread pool for handling clients, so it wouldn't be an issue and would (IMO) still be easier to understand. To me the async model feels weird, and hides what is going on more, which I don't consider really a good thing when there are multiple things going on. And it lets you be sure you have a thread per client (if you want that), whereas that's not the case in the async model, where I assume you are depending on some internal heuristics as to how many threads are being created and when. The thread pool based system has a much more controllable response time to connections.
    A generic interface for waiting on things (and for things to make themselves waitable) of course is simple and obvious, and can be used in many ways, without the obfuscation of the async system. And for those things that you really are likely want to want to wait on already probably have OS level (and wrappable by the runtime) mechanisms to have them be done asynchronously and signaled, so you can easily make those waitable without any of the overhead of the async system.
    As a cooperative multi-tasking, it's really not useful for latency sensitive operations, except when everything it does is truly async and very light weight, and if you have to carefully ensure such things, what have you really gained in terms of simplicity? And how easy would it be for code changes to introduce something not light weight without it being at all obvious from reading the code?

    • @cat-.-
      @cat-.- 3 ปีที่แล้ว

      If you have a thread pool / green threads, you already are in the woods of cooperative multitasking. All the technical drawbacks of async/await apply, and the choice is simply about taste/style. If you want true OS threads it proves to be too much of an overhead (for example Apache) to be competitive against an async/await high performance server (e.g. Nginx)
      And yes, it is true that async await and coroutines in general is only good if your application is really low on compute, and dead heavy on IO, but that is really what a web server is, for like 99% of the time

    • @deanroddey2881
      @deanroddey2881 3 ปีที่แล้ว

      @@cat-.- And there are now many web servers in the world, meaning real ones that need huge I/O throughput, not simple ones in devices and such? They would be about 0.0001% or some such of applications. And could still achieve the same thing even lighter weight than green threads by directly using async I/O.

  • @d0lvl0
    @d0lvl0 2 ปีที่แล้ว

    Is there much support for debugging with async in rust? I'm not able to get break points to trigger in async functions.

  • @chrisnocker3437
    @chrisnocker3437 3 ปีที่แล้ว

    Thanks Jon, learnt a lot!

  • @김태현-o5o6f
    @김태현-o5o6f 2 ปีที่แล้ว

    So, with future and async/await, non blocking IO can be possible with only one thread? Or, is It possible without support of future and async/await ?

  • @ZhanhuiLi
    @ZhanhuiLi 2 ปีที่แล้ว +1

    So under the hood, rust, go, and other co-routine-like features are still using EPoll on Linux, kqueue on Mac, IOCP on Windows.

  • @ketanchaudhari2337
    @ketanchaudhari2337 2 ปีที่แล้ว

    If a executor have 2 future spawned and are in epoll ready, one's f.poll() return Ready and done. How will the executor return the ready value to the .await. and will/if it continue after .await on that future?. or will it wait for 2nd future.

  • @omgExodus
    @omgExodus 2 ปีที่แล้ว

    Great talk! 👏

  • @cthutu
    @cthutu ปีที่แล้ว

    Fantastic talk - the only thing I would say is missing is more detail on how to deal with dynamically sized selects (i.e. the number of futures varies over time). Are Futures(Un)Ordered involved or tokio streams?

  • @try-h6f
    @try-h6f 11 หลายเดือนก่อน

    Thanks for the brief

  • @mback3713
    @mback3713 2 ปีที่แล้ว

    I'm interested in your vim configuration -- what extensions are you using & how did you set up your dev environment so nicely!?

  • @parthikpatel6108
    @parthikpatel6108 3 ปีที่แล้ว

    In the functional programming world we put our "effectful" operations in an "effect" type (Like cats IO in Scala)... so is it fair to say that in the Rust world, corresponding idea is to use futures and async/await?

    • @jonhoo
      @jonhoo  3 ปีที่แล้ว +5

      Ah, no, this isn't a way to describe whether an operation does I/O or not. Synchronous code can also do I/O. It is solely a matter of using a different execution model.

    • @parthikpatel6108
      @parthikpatel6108 3 ปีที่แล้ว

      @@jonhoo thanks!!

  • @dim-2d
    @dim-2d ปีที่แล้ว

    This is probably a very stupid question, but in foo2() why do you want to “sleep” until a future is resolved? I mean why not just block? What’s the benefit of going to “sleep” and being woken up by the OS if you’re in a non UI environment like a CLI? Is it because you’d save on computational cycles that would normally be wasted? Again, sorry if it’s a stupid question. I understand the use case with select!, but not in a “regular” code.

  • @nteasushz
    @nteasushz 3 ปีที่แล้ว

    Great video. Thanks for sharing.

  • @samuraijack7295
    @samuraijack7295 3 ปีที่แล้ว

    I didn't get a YT notification for this :(. I would have liked to ask whether it was normal for tokio select! to poll without being explicitly notified by a waker? I have a delay_queue the impls Future and it gets several extra calls to poll by select! before select! actually sleeps and waits for the waker. It feels kinda like debouncing.
    I would expect it the poll function to be called twice. Once on init and then finally when the waker notifies the executor and the future completes...

  • @parthikpatel6108
    @parthikpatel6108 3 ปีที่แล้ว

    So are futures Rust's answer to green threads? if not, what is the difference between green threads and Rust futures?

    • @jonhoo
      @jonhoo  3 ปีที่แล้ว +3

      Well, sort of. Futures aren't themselves green threads, since they don't actually run on their own, but when combined with an executor you effectively get green threading.

    • @parthikpatel6108
      @parthikpatel6108 3 ปีที่แล้ว

      @@jonhoo makes sense, thanks!

  • @furkanaksoy8180
    @furkanaksoy8180 3 ปีที่แล้ว

    Man, i wish you run the programs you wrote. Content is great but seeing them in action makes me understand the process better.

    • @jonhoo
      @jonhoo  3 ปีที่แล้ว +5

      I'm curious what you feel like that would add in this video? I usually do run my code, but here most of what I've written is more about the types, and running it wouldn't actually print anything 😅 For much of it, there'd also be a bunch of not particularly interesting extra code to write to get things to compile, which would make the video probably twice as long without actual contributing any further insights or understanding.

  • @davidfranco7480
    @davidfranco7480 2 ปีที่แล้ว

    Hi, do you often do rust lives like this ?

  • @tanjack9484
    @tanjack9484 2 ปีที่แล้ว

    Hello, I saw your video on TH-cam above and thought it was very good. I am from China and would like to translate your video into Chinese subtitles for Chinese video sites, what authorization do I need?

    • @jonhoo
      @jonhoo  2 ปีที่แล้ว

      Hi there! You don't need any authorization. The videos are licensed under Creative Commons specifically for this purpose. Please provide a link back to the video and/or to my Twitter though :)

  • @Dygear
    @Dygear 2 ปีที่แล้ว

    With the select! macro, lets say I have 2 things that I'm waiting on. The first is a TCP socket that gets data from the network in a highly variable manner. Sometimes I get packets from it between 1 second to 1 hour. (This also means that I have to keep the connection alive by sending it a ping to the server every 30 seconds.) The second is a UDP socket that gets data from the network in a much more consistent manner. I get a packet from there between 50 milliseconds to 6 seconds. Both of these packets are getting parsed out and saved to different SQLite databases on the backend. The TCP one is doing a lot of computation on that packet and indeed could make another TCP connection to another server, or send a HTTP request to another service this takes a lot of time. Is there a guarantee within the runtime that the if I get another TCP packet or UDP packet before that computation is done that it will also handle the next TCP packet as well? Or am I going to spill the next packet on the floor as it flows past what my program is able to handle. What about the UDP side that is waiting for TCP to handle its packet. Given that the TCP side could take several seconds to finish its processing of that packet, would I be dropping UDP packets on the floor because my TCP future is running and hogging the system?

    • @Dygear
      @Dygear 2 ปีที่แล้ว

      Partially answered at 55:35 - You would need to call .await on these expensive functions call in the TCP socket to make sure that it yields back to the select call. What it does't answer is if another TCP packet comes in, would that TCP packet get processed as well, or would the new TCP packet clobber the handling of the previous packet that is still in flight, or would it drop that packet, or would it queue the packet for the next iteration into the select macro call. -- The solution (but not the answer) for that comes at 1:27:00 where they talk about giving that packet (or connection in their example) to the executor for them to handle and make progress on until it's done.

  • @petrupetrica9366
    @petrupetrica9366 3 ปีที่แล้ว

    I considered rust for writing a highly asynchronous I/O heavy application, however I ran into trouble when I was trying to await on a few lists of futures of different types. Additionally, at the end of a task from a list sometimes I would add some more to one of the other lists, meaning that I had to really select on these lists and depending on which task was finished, possibly modify these lists. I am new to Rust so I don't know if I was missing something but I found no way to do just that (selecting on multiple lists of different types of futures), do you know if / how it could be done?

    • @jonhoo
      @jonhoo  3 ปีที่แล้ว +2

      The way I'd do this is to tokio::spawn each future, and then add all the returned join handle futures to a FuturesUnordered which you can then pull from.

    • @petrupetrica9366
      @petrupetrica9366 3 ปีที่แล้ว +1

      @@jonhoo Thanks, I used std async just because I thought it's the standard having std in the name :)). But I see tokio is really the most used and fully featured one, I'll try again with tokio and see what results I get.

  • @gesprengt6561
    @gesprengt6561 2 ปีที่แล้ว

    So if you only ever use await and never spawn or join, the program behaves sequentially?

  • @comradepeter87
    @comradepeter87 3 ปีที่แล้ว +1

    I feel really weird that the futures don't really execute until .await is called on them, while there is a background worker thread that could've taken that future and polled it to completion. Are there any executors that immediately start polling the future (in a background thread) as soon as they are made? (Eg: the future in its construction pushes itself to a worker_queue that the executor pops the futures out of and distributes them to worker threads).

    • @comradepeter87
      @comradepeter87 3 ปีที่แล้ว +1

      This model of not executing a future immediately only works if you have a lot of futures to poll/execute so that the background thread isn't really wasting time when the future wasn't .await-ed .

    • @jonhoo
      @jonhoo  3 ปีที่แล้ว +1

      No, there is no such executor. And there couldn't be - there's nothing special about the creation of a future that an executor could hook into. In fact, even await doesn't actually make a future run. It just says that *when* this future runs, and gets to this point, it must wait for this other future. It's only when a future is given to an executor that it actually runs.

    • @comradepeter87
      @comradepeter87 ปีที่แล้ว +1

      ​​@@jonhooWell yes, you are right in that Rust doesn't have some notion of "default constructor" that gets run, which would've made this elegant. However at the expense of more boilerplate, couldn't we write an extension method on Futures, say .run(), that spawns the future in a new Task/parallel compute, and returns some sort of handle that you can call .await on? Would it be possible to do this without too much synchronisation problems?

  • @ricardopieper11
    @ricardopieper11 3 ปีที่แล้ว +2

    The windows IO thing is IO completion ports IIRC

  • @protyaybanerjee5051
    @protyaybanerjee5051 3 ปีที่แล้ว

    Great channel. Found your reference from the Demikernel paper.
    What is this Vim setup anyway? Looks great

  • @karelhrkal8753
    @karelhrkal8753 2 ปีที่แล้ว

    Never seen the "select" macro, the JS way would be to add .then() on each promise and then do Promise.all(). That way, you can still react to each individual promise resolving but you also have guarantee that the process will not continue until all promises have resolved.
    Not sure how that would work in Rust. Event if you added a .then() function on a future, I guess there might be some complications with the borrow checker. I will have to try it out to find out.

  • @alvinkuruvilla5163
    @alvinkuruvilla5163 3 ปีที่แล้ว +1

    2 streams in 1 day? Christmas came early :)

  • @thalyssonleite1479
    @thalyssonleite1479 ปีที่แล้ว

    A Masterclass

  • @LongestYardstick
    @LongestYardstick 2 ปีที่แล้ว

    Just bought your book.
    I love the syntax highlighting here, is this a popular vim color scheme?

    • @jonhoo
      @jonhoo  2 ปีที่แล้ว +1

      It's called base16-gruvbox-dark-hard

  • @yushie8158
    @yushie8158 2 ปีที่แล้ว +1

    Awesome tutorial!

    • @jonhoo
      @jonhoo  2 ปีที่แล้ว

      github.com/jonhoo/configs/blob/master/editor/.config/nvim/init.vim :)

  • @colin398
    @colin398 ปีที่แล้ว

    With C++ futures, its interesting to note that there is different behavior:
    #include
    #include
    auto f() -> std::future {
    return std::async(std::launch::async, [] {
    std::cout

  • @jearsh
    @jearsh ปีที่แล้ว

    "rejiggered" ...i'm stealing this

  • @OBLIVIOUSKARI
    @OBLIVIOUSKARI 3 ปีที่แล้ว

    What’s the difference between tokio::spawn and spawn_blocking?

  • @lnv-lnv
    @lnv-lnv 3 ปีที่แล้ว

    Thank you for your videos.

  • @timanderson5717
    @timanderson5717 3 ปีที่แล้ว

    How do panics work in async code? Can you catch panics from a spawn or something?

    • @jonhoo
      @jonhoo  3 ปีที่แล้ว

      Depends on the executor. I believe Tokio will generally catch panics to prevent the executor threads from terminating, and then propagate that to join handles, but the documentation should say.

  • @АйбатАманбайұлы
    @АйбатАманбайұлы ปีที่แล้ว

    What are you typing on? Looks like vim. Where can I look it up?