Is Using Async and Await a Good Choice? - .NET 150
Adam, Mark, and Shawn this week's panelist episode to talk about "Async Wandering Part 8 — async and await — the biggest C# mistake?". Adam takes the lead as he explains why he thinks "Async" (one of the most popular C# features) has some issues or downsides and how it affects your application.
Show Notes
Adam, Mark, and Shawn this week's panelist episode to talk about "Async Wandering Part 8 — async and await — the biggest C# mistake?". Adam takes the lead as he explains why he thinks "Async" (one of the most popular C# features) has some issues or downsides and how it affects your application.
On YouTube
Sponsors
- Chuck's Resume Template
- Raygun - Application Monitoring For Web & Mobile Apps
- Become a Top 1% Dev with a Top End Devs Membership
Links
- Async Wandering Part 8 — async and await — the biggest C# mistake?
- Understanding the Whys, Whats, and Whens of ValueTask
- Parallel Computing - It's All About the SynchronizationContext
- .NET Threadpool starvation, and how queuing makes it worse
- Blogging about Midori
Picks
- Adam - CentBrowser
- Mark - Photoshop Generative Fill
- Shawn - FUBAR
Transcript
Shawn Clabough:
Hello and welcome to another episode of Adventures in.NET. I'm Shawn Clabough your host and with me today, co-host Mark Miller.
Mark Miller:
I did
Shawn Clabough:
Hey
Mark Miller:
it!
Shawn Clabough:
Mark.
Mark Miller:
I'm
Shawn Clabough:
Hey! Hey.
Mark Miller:
here. It feels like it's almost real. Like,
Shawn Clabough:
It's...
Mark Miller:
I think I'm like a regular now all of a sudden. I'm starting to feel it.
Shawn Clabough:
Mmm, so AR or VR with you?
Mark Miller:
What? Uh, I guess AR. That's
Shawn Clabough:
Hehehehe
Mark Miller:
probably the best. I think I think we need some, some wimpy kids in here that are like, I just want to be a host so I can be like, kids, you gotta, you gotta be like me on my dues. I paid my dues like that. Something like that. I don't know.
Shawn Clabough:
Okay, we'll figure it out. Alright.
Mark Miller:
Thank you.
Shawn Clabough:
And we got Adam Framonic. Hey Adam. Yeah. So you've been out a little bit. You're doing some conferences. Wow, how's your frequent flyer miles doing? Hehehe. Yeah, lots of fun. Yeah, I just got back from a trip myself, but it wasn't that far. My wife and I went to Wisconsin to visit the family. So it was a quick five-day trip and didn't get many miles out of that. I think I'm good, so. So I think what we're gonna cover today is an article that you wrote, Adam, and it's dealing with async, async awake, and why it's just one big mistake. You know, I think that might be debatable for some people out there, but I think, I think, I think you could probably convince us otherwise in why we should go through all of my code and take it out. So where do we start? Okay, well I think Mark and I do remember the day where we were walking around with the T-Rex, so yeah, we are dinosaurs.
Mark Miller:
Yeah, I kicked it in the nuts, man. It was the last one. I was the one who ended the dinosaurs.
Shawn Clabough:
Just stay out of arms reach, that's it.
Mark Miller:
Okay, yeah, just, that's
Shawn Clabough:
Okay.
Mark Miller:
true.
Shawn Clabough:
All right, so first thing you talk about, you say it's expensive. So what do you mean by expensive? So machines have gigabytes of memory nowadays and even more than that. So a couple hundred bytes, what's the problem with that? So is that in and of itself a reason to not use async? And does it make a difference if you're doing like a desktop application versus a web app? Yeah, because one real good benefit of using Async on a desktop app is not locking up the GUI. You used to have to use the background task to do anything that you still wanted the GUI to be responsive and things like that. And so using Async really has made that a lot easier. So there's probably trade-offs depending on what you're doing. So you've got everybody that's listening has to decide for themselves, what's more valuable to them? Some of the ease of use of Async, the benefits versus the costs that you're going through today. Yeah, yeah, yeah. OK, so the next section of your article talks about, you know, it doesn't integrate with the rest of the platform very nicely. So where do we where do we go there? I definitely have to agree with you there. That was one thing that really drove me crazy when I was starting to implement async and all my code. And that's being able to, or needing to go all the way up the chain to get to very top and then start it from there and then go all the way down. You know, it also, you know, dependency injection could be that way as well sometimes. So that does drive me crazy, but yeah. I've seen configure weight so much over the years and things like that and never really had a good understanding of, okay, when do I make it true? When do I make it false? You know, and then is there a way for me to do this globally? Or do I have to do it on every single statement that I'm doing this? And I think it's you have to do it in every single statement, right? Okay, yeah, I don't think I've ever actually put it in my web apps and controllers and things like that. But if you're saying don't use async, then you don't have to worry about this problem because you won't need to configure a wait. Anything to add, Mark?
Mark Miller:
Sorry, no, I had it muted because I had a car driving nearby me. Yeah, I do have something. Hold on. So Adam, I'm like, you know, kind of clueless on configure a weight false, the meaning of it. I kind of understand a little bit, but I feel like I'm missing maybe a lot of foundational information on this. I think what I want to ask is what's the stupidest way you could explain this to me so I could retain it? and kind of work with it, right? As a guy who maybe doesn't know everything that's happening under the scenes.
Shawn Clabough:
Thank you.
Mark Miller:
OK, so if I got this correct, if whatever is the continuation, the piece that's coming up afterwards, that code, if that code can, if I determine that code can safely run in parallel, then I want to say configure await false. And if I'm concerned at all about it, then I don't add that configure await call to it. And then it'll go on the synchronization context. Do I kind of have that right? Oh my god. Okay. That's
Shawn Clabough:
and the
Mark Miller:
amazing.
Shawn Clabough:
synchronization context has access to the GUI thread.
Mark Miller:
Yes. OK. Adam, I got a second question for you. I heard you say that you can't do an extension method. But I'm not sure. So the configure a wait call, is that on a task? Is the object you're calling, what is it on? Ah! Ugh, son of a... Damn, okay. Because I was thinking you could create a real simple, you know, just so you can type fewer characters, right? But you cannot do that. Wow. I'm going to add a CodeRush template for it. So I just type in CAF and then hit space bar and it does everything for me. That's where I'm headed.
Shawn Clabough:
Hehehe
Mark Miller:
Adam, you're brilliant. That was awesome. I'm now a genius everybody. Come ask me questions on this next.
Shawn Clabough:
So what does Code Rush recommend for async? How does it?
Mark Miller:
Code Rush doesn't have any recommendations on it that I'm aware of. I don't think we have anything on there. But I will say I've totally experienced the async all the way up, right? But I didn't mind that actually. I was like, well in part, now Code Rush does have, well I think Code Rush has these. I'm not sure if we're shipping these or these are my own, but I've got like one letter keywords, templates, rather. that allow me to introduce async just by hitting the letter Y in a space bar at the right place in the code. So if I do Y in space bar, it'll put in the word async for me. And if I do a W space bar at a place where an await makes sense, it'll write the word await for me. So when I did the async all the way up, it wasn't hard to do. I was just, it was just a few keystrokes. And I actually liked seeing the impact. It felt like almost a learning experience for me to get a sense of... of what that impact was and that cross section through the code, right, that I was changing.
Shawn Clabough:
Yeah. Next, you talk about in your article something that a lot of people have probably ran into and that's when they have a method that needs to return nothing. So most people will make it an async task, just generic task, no return type, things like that. But some people struggle with, could I just use void? So. Okay, so um... One thing I've ran into a lot of times when I'm doing async in my like little test console apps or using link pad or things like that is being able to get a good grasp on any exceptions that are thrown because the exceptions are gonna be thrown in another thread. So it messes with the stack, right? And you really can't, it's hard to get the full details of any exceptions that happen inside an awaited task, right? Okay, so the next things you cover in your article are kind of some odd things that I never really thought about. So you're talking about here, you're talking about, it's talking about, it can change a thread. So in my personal code, where I tend to use locks is mostly in my caching layer. So a caching layer, caching class, or anything like that, should that avoid using async? Interesting, interesting. Okay, so the next section of your article talks about hidden state, you know. Why would I care that it has a hidden state?
Mark Miller:
I have
Shawn Clabough:
Okay.
Mark Miller:
a question, Adam, on that. I guess it seems to be that if you have control over all of the code, that that's not really a concern. It's only a concern if you don't have control over the whole code. For example, in an architecture that supports plugins or something like that, is that assumption correct? Or is that not correct? Do you think that even if you have control of all the code, you're still opening yourself up into a kind of a dangerous way of working.
Shawn Clabough:
Okay, the last section of your article talks about async doesn't follow best practices. So take us through that.
Mark Miller:
So,
Shawn Clabough:
Well, I do. Go ahead,
Mark Miller:
sorry,
Shawn Clabough:
Mark.
Mark Miller:
go ahead, Sean.
Shawn Clabough:
No, I just want to say that I'm definitely a guilty person of writing synchronous and asynchronous methods doing the same thing. Because I was trying to migrate things from synchronous over to asynchronous on an existing code base and things like that. So yeah, I definitely did that. But now I'm going to get rid of all my async code. I won't have to do that anymore, right? What was your question, Mark?
Mark Miller:
I guess, Adam, I'm thinking about where do I go from here? So I'm not the guy that knows everything that I need to do to turn my async code into essentially equivalent code where I have control over everything, right? It seems like in order to take a step in the direction that you're recommending, I guess what's my first step? Do I have to get that foundational knowledge first? Is there a way that I can make a gradual step, like a best practice kind of step? What do you recommend? Okay, I love that answer, Adam. That's definitely an actionable item for me. I have another question for you. You're talking about performance issues, right? And it seems to me that it might be a situation where you cannot actually detect that async and await is the core of the problem until you actually try doing it the other way and comparing. Is that true? Is there a footprint, something I can see in a profiler that tells me, oh yeah, my garbage collection is high. It's likely all my async and await calls.
Shawn Clabough:
Okay, so as we wrap up here, are there any situations where you would actually recommend that somebody use async? Okay, so use async, but don't use async. Yeah, got it. So all my Entity Framework method calls are still good. Did I leave them async? Okay, that's good. Anything else to add? Got it. Okay. Alright, so let's move on to picks for this week. Mark, what's your pick?
Mark Miller:
My pick is generative fill in the Photoshop beta This is an incredible feature And and I will say that it's not without some weird quirks But I was using it just before we started the show today I started with a close-up of a cactus and I wanted to get a wider shot of it so I Stretched the canvas out I selected the area on the outside of the picture, and I said generative fill, and the feature, if I don't tell it what to put, it'll just try to continue and fill it in. And it builds, it built the leaves of the cactus outside of the frame, it built the ground around outside where the cactus was there, did it all in about, I wanna say, 20 seconds, 25 seconds. I was so amazed. You can... And you can also give it text prompts type it in what you want to be in that space. I did like I said, I had a few weird moments. One of the weird moments was one of the tips of one of the cacti that had built were the cacti leaves or the edges of it was kind of brown and didn't look as fresh as the others right is full of life. And so I said, you know, fix these make them have the same red tips as the rest. And it put on some cap from a. handle or something on the end of the flower. And it gave me three alternatives that were all like that. And I went back and scanned my text prompt to see what did it misunderstand and I couldn't find anything. So it didn't always come up with amazing stuff, but often it really did. And I really, really liked it.
Shawn Clabough:
Yeah, I definitely I'd agree with that. I've seen some of the videos on that. And you guys probably don't know I used to have a side business doing real estate retouching. And often people are saying you add this to this, or replace this or, you know, quite often the grass was like terrible and after place the grass or replace the sky with blue sky and things like that. And so having this kind of a tool for that kind of use would be would have been awesome.
Mark Miller:
Yeah,
Shawn Clabough:
But that was a
Mark Miller:
it
Shawn Clabough:
few
Mark Miller:
allows
Shawn Clabough:
years
Mark Miller:
you,
Shawn Clabough:
ago. Yeah.
Mark Miller:
it allows, yeah, in the old days, it would be like, do this thing, here's what I want you to change, we're both imagining it, then you would go off, make the change and bring it back, you know, sometime later. Now, you can do it together, sitting there, you know, do some what-if scenarios. It's incredibly productive. Yeah, I really, really was impressed. I was impressed with the implementation of the feature. It's incredible. I will also say that I've seen the videos and it was there was something about doing it firsthand that made it way more impressive and way more exciting, way more thrilling. Really, really impressive feature.
Shawn Clabough:
Yeah, my claim to fame is I've done real estate retouching of properties for Kelly Ripa and Queen Latifah and Rafael De Niro, you know, that kind of people. I didn't work with them directly. I was working with the photographer that did the pictures, but, uh, you know, I'm
Mark Miller:
Dude,
Shawn Clabough:
only
Mark Miller:
you
Shawn Clabough:
a couple
Mark Miller:
can
Shawn Clabough:
of
Mark Miller:
set
Shawn Clabough:
D
Mark Miller:
me up! You can set me up! I can meet these famous people!
Shawn Clabough:
I've only got a couple of degrees of separation, but they
Mark Miller:
We
Shawn Clabough:
don't
Mark Miller:
can
Shawn Clabough:
know
Mark Miller:
do
Shawn Clabough:
that
Mark Miller:
it together.
Shawn Clabough:
they don't know it. Yeah,
Mark Miller:
Next
Shawn Clabough:
we can
Mark Miller:
show
Shawn Clabough:
do it.
Mark Miller:
will be about all my exploits, finding all these people, pointing
Shawn Clabough:
Yeah.
Mark Miller:
out the things that you added to the shot.
Shawn Clabough:
Yeah. Oh, yeah. It's amazing. I've seen some pictures just show up on the Internet. They say, hey, I did that. It's like. Because a lot of the places that I did were in New York City, so lots
Mark Miller:
Cool?
Shawn Clabough:
of there. All
Mark Miller:
That's cool.
Shawn Clabough:
right. So my pick this week is another Netflix show. It's FUBAR and it's got Arnold Schwarzenegger in it. And, you know, it's not the greatest thing, you know, ever. But it was entertaining. It's got a kind of a reused storyline. It's a CIA agent that's kind of going on one last job and ends up finding out that his daughter was also a CIA agent and he didn't know about it. And she didn't know about that he was that. And she just thought he was just this bum dad that was always traveling and showing up late for things, whatever. But...
Mark Miller:
That's what my kids think about me that last part.
Shawn Clabough:
Yeah. Yeah, so I mean, it's they call it an action comedy. So but so it's good. It's entertaining. It's a it's a small series that would probably do another one. I think it was successful enough. So if you're looking for something, you don't have anything else to watch. Take out FUBAR on Netflix. All right, Adam, what's your pick? Okay, thanks Adam for giving us the rundown on Async today. That was very, very informative and instructive to me. So really, I have to really think about to you know what I want to do, what I want to do now. So, all right. As we wrap up, I'd like to let Listeners know if they want to reach out to the show they can touch it get in touch with me on Twitter I am at dotnet superhero. We'd love to hear your feedback and any suggestions of Future shows that we talked we should talk about so thanks very much and Catch everybody on the next episode
Is Using Async and Await a Good Choice? - .NET 150
0:00
Playback Speed: