Placeholder Image

字幕表 動画を再生する

  • what's going on, everybody.

  • And welcome to another intermediate python tutorial.

  • In this tutorial, what we're gonna be talking about is a sink io or sink, Yo, depending on which way you want to say it, I won't judge other people will, Uh And what a sink iose for is for a synchronous programming.

  • And probably the best way to thio kind of understand concurrency.

  • Um, in basically any programming language is it's often confused with doing something in parallel.

  • It's kind of like you're handling maybe multiple things in parallel, but you're definitely not doing them.

  • You might.

  • You're aware of them in parallel, I suppose.

  • But you're not doing them in parallel.

  • So, for example, like, let's say you're you're out for a jog and you feel like a little tap on your shoe.

  • You look down, your shoe's untied.

  • Do you have to finish your entire jog to tie your shoe?

  • I hope not, Right Like that would be That would be unsafe to your very unpleasant and unsafe at the same time.

  • Eso No, you stop your run, you suspend your run and you bend over entire shoe.

  • You finish tying your shoe and you get back to your run, okay?

  • And this is your doing these two tasks.

  • What right to your able to tie your shoe and you're able to run concurrently.

  • So the same thing is true with programming like, let's say, you got a Web page, and that Web page needs to load a whole bunch of elements.

  • But, you know, as it's going down the page and it gets to some image that it's requesting from some server, that server just is not responding.

  • What happens?

  • Well, if your pages loading synchronously where your entire Web page will hang.

  • But if your page loads a synchronously, then the other elements are going to get to load.

  • And you may or may not ever love that image.

  • Maybe that servers down or something.

  • But if it's just taking a long time while you have that Leighton see between the the io had hence a sink, I owe input output.

  • While you have that delay, you could be doing other things other than just sitting there literally doing nothing and waiting on, um ah, response.

  • Now, um, what I'd like to do here is, um, just I think we're just gonna start with a really basic function, and then we're gonna turn it into, um ah ko routine.

  • If I say go routine in this tutorial.

  • Forgive me.

  • Um, my first experience with concurrency was actually with go routines and go, and so I just call it co routines.

  • Go routines.

  • You might not even notice.

  • Anyways, um, let's just say this is the function that we're going to use, So all it is is it's just gonna find some numbers.

  • And by the way, yes, I'm just I just copy and paste to this function.

  • Um, e I'll put a link in the description to the text based version, and you can get it.

  • Grab dysfunction where you can write it as I'm talking if you want.

  • But basically, the idea of this function is to just in any range we're gonna divide by something.

  • So, uh, then what we're gonna do when we start the function is we're gonna print out what we're doing.

  • What's the range?

  • Was the divx by?

  • We're going to start a located.

  • So anything in that range, that's ah, basically, that's divisible by whatever r div by is, we're gonna add that and then when we're done.

  • We're gonna have a little thing that says we're done, and it returns.

  • Whatever we located now to run this, um, I'd probably just put it in a block.

  • But for the purposes of this tutorial, we're gonna go ahead and we'll use Define.

  • Um, we'll just have a main main function here, and we'll just say dibs.

  • One equals.

  • And let's just, uh, copy and paste that and that will be just so I match the text based right up.

  • Let's do 50 8034 1 1133 and then I'm just gonna copy.

  • Paste paste.

  • Make that 23 uh, 1050 to 30 to 10 and then we'll do 500 three.

  • Okay, so now we've got our main function here.

  • And then, um, just for in case someone wanted to import this amazing script of named equals main, we would have just let's just run the main function.

  • So, um, so with that, you also you know, you can change this function to be anything you want.

  • You can you can have it doing Ah, Fibonacci stuff.

  • If you really want to be like everybody or he could do, Um, anything you want.

  • Really?

  • I will say, if you are interested in doing truly http, eat tasks, you can look into a Iot HDP for doing a synchronous requesting and stuff like that.

  • So that's why we're not going to use that for this example.

  • But just take notes.

  • So, uh, now that we have this does want to, um what we want to do is we could even let's just run this real quick.

  • Just see what we're dealing with.

  • So as you can see, this whole script runs.

  • It's about 0.3 seconds, and we can see it runs in the order that we called it.

  • Basically So runs dibs.

  • One it finds it just starts iterating through 508,000 finding all instances where we have a number that is divisible by 34,113.

  • So it does this one and then it does this one, and then it does, uh, this one.

  • So what could we do to convert this to be a synchronous?

  • And why might we want to do that?

  • Well, for example, what if this was instead this right?

  • And we ran this?

  • It's gonna hang on this one where we actually really could have found the answers to these two and then come back to this one and spent aboutthe same amount of time, right?

  • We didn't have to waste all this time still waiting on this one, right?

  • And so So that's kind of what we're trying to solve.

  • We're trying to figure out a way that we don't need to do this.

  • And we'd ideally not like to use something like threading or multi processing if we don't have to, because that ads in a whole slew of other things that we might may or may not be interested in dealing with.

  • So, uh so So at this point, this program is totally synchronised.

  • It runs in order and all that.

  • And now what we want to do is begin to bring this, uh, Thio, bring a sink.

  • Io into this summer's gonna bring over the documentation again In the text based version of Suit Auriol.

  • I do put a Lincoln here, but as you can see, this is a pretty large package.

  • So there's a lot going on here.

  • In my opinion, I always wish in the standard docks they would have like a you know, stupid, simple section, right?

  • Just first, like super basics.

  • And then you could go through this stuff is that's basically what we have to do every time we find something we have to do.

  • Like a really simple example.

  • Uh, and then and then we can.

  • And then we could maybe come to the docks and see what?

  • What can we tweak and change and all that, But anyway, there's a whole bunch there for you is basically an a p i over concurrency that you really could use yield from.

  • You could use the go code routine decorators and stuff like that, and you wouldn't necessarily.

  • You don't have to use a sink I owe to do concurrency.

  • It's just it's a very large package to help you do it.

  • So anyways, getting back to the code, How do we convert something to being a synchronous?

  • Well, first of all, the main conversion is rather than deaf.

  • Something you a sink death something.

  • Um, and that basically notifies python A.

  • This is a co routine.

  • So the first step that we're gonna make is actually we're just gonna a sink both of these functions now Uh, the next thing that we want to do is we want to go ahead and bring lips.

  • We're gonna bring in, um, import a scene guy.

  • Oh, that way we can use some of these like, helper helper functions, basically with a sink.

  • Io.

  • So now what we want to go ahead and do is once you have your co routines, we need some sort of loop, right?

  • Right now, we're just running main and in theory, main itself.

  • You would call this at the moment?

  • Probably a main loop just because it's gonna run through that little block of code.

  • But what we want to do with a sink Iose we need to establish the main loop, which is kind of it's basically your event loop.

  • And then you were gonna add in tasks and tell that loop how to run and all that.

  • So so now we're gonna do is we're gonna come down here, and this is where we're gonna stuff in the least the initial definitions for loop.

  • So we're gonna say loop equals a sink io dot Get underscore Event blue.

  • So this is just gonna initialize our loop for us, then what?

  • We want to dio is we basically want to run the main co routine, Uh, until it's done.

  • So the way that we're gonna do that is we're just going to have its just its loop dot run, run, underscore until underscore complete main.

  • And again there's there's, like, run things run forever.

  • Yes, right here in the documentations.

  • Uh, yeah, you could run forever.

  • You could run until complete.

  • And then there's all kinds of checks that we can do.

  • We can get status and all that.

  • So again, there's no way I can cover this entire package in one video.

  • Um, or even a few videos just know the docks exist, So so anyways, run until complete.

  • And then whenever you're done, um, you can you do a loop dot clothes.

  • I'm actually not 100% confident.

  • Run until complete is gonna require a loop dot clothes, but we'll go for it also, you'll see a lot of times in code, it's going to try to do the loop stuff except do some logging, not really a pass.

  • And then finally, you're gonna loop dot clothes because you always want to close off that loop.

  • This is telling me is Brian Griggs.

  • Don't do anything too, Okay.

  • Exception has he?

  • How about that?

  • You don't like the exception?

  • Cool.

  • General exceptions.

  • OK, Anyways, um okay, so So now we've done s We've got the loop.

  • Now we have to do is we need to insert stuff into that loop.

  • So all it's gonna do right now is we're on the main core routine.

  • Okay, but the co routine is running.

  • Basically, these functions still synchronously, right?

  • It still says just run 12 and three instead we want to do is we're gonna add them in.

  • So rather than find divisible sze, we want to say, rather than just running just that function, we actually wants a loop dot Create underscored task.

  • Take three.

  • There we go.

  • Run that.

  • So now I'm just gonna add this bang bang, bang bang.

  • And now we've created the task in the loop toe, actually, run these three things now.

  • I'm trying to think I think I think this is Runa below at this stage, let me just run quick, see if we had there.

  • Yeah, okay.

  • It is Okay.

  • So it is run noble at this stage, but we have a couple of issues.

  • The first thing is, any time you you send off a co routine to run, you want to make sure you wait for the code routine to finish, especially if you're gonna start in a loop or if you're gonna end your code.

  • So if you're just if you're writing a script that happens to use a stinky Oh, if you happen to need to do that, you want to make sure you've waited for the results before you end this script, Um, or but again in the loop, especially if you don't wait, you're gonna you're gonna really quickly dispatch a lot of code routines.

  • So So what do we need to do?

  • We need to await.

  • So we await pretty much you're gonna await batches or single co routines trying to think if I'm gonna say something wrong here, but I'm trying to have an instance where you wouldn't want to eventually await your co routines.

  • If you can think of one, let me know.

  • I'm sure there's gotta be something, but anyways we want, especially if you want the least.

  • If you want the results, you're gonna need to wait for it.

  • You're just trying to dos somebody.

  • I guess you don't need to wait for it.

  • So anyways, ah Ah, wait.

  • And then we're gonna sink.

  • Eo, I gotta stop saying thank you.

  • I'm going to get stuck saying it now.

  • Uh, dibs.

  • One dibs to I see my type of dudes.

  • Three interested.

  • Why, though?

  • There it isn't.

  • Sees it.

  • I did to him.

  • More coffee, please.

  • So now we're gonna do is we're gonna wait on those code routines to run.

  • Now, if we run this right now, though, we're probably going to see Yeah, still runs everything synchronously.

  • And if we were to let's just make this one order of magnitude, okay?

  • Two orders of magnitude Coming right up, sir.

  • Area.

  • So it's still it's still hanging.

  • Right?

  • And they were like, What's going on, man?

  • So the problem is, even though this is a code routine, everything in here, though, is still synchronous like there's really no point where we actually have No I Oh, here we have no Well, okay, we have io, but not not like highlight in C i o.

  • We were just running this task.

  • So in this case, this is a book task.

  • This isn't really a request to a server.

  • What?

  • We wait for a response, And then during that, wait for a response, we could do something else.

  • Um, so instead, what I'm gonna do is we're just gonna add a loo.

  • Tiny bit of weight.

  • Basically, um, right here.

  • So So in this case, we're just as we generate through that range were asking, Is it divisible by what that did by is if it is, we're gonna upend it to located but the other, we're gonna have one more thing.

  • We're gonna see if I ma djalo.

  • Um And then in this case, we can use anything.

  • Um, it depends on really with this big task is like you you would probably want to logically do this.

  • You could make it a number as large as 100,000 or larger than 100,000 and then this would never get hit.

  • Um, and I'll show if that's not if you're following my logic yet.

  • That's okay.

  • So let me actually just write it out.

  • So say 50,000.

  • So if I is divisible by 50,000 then what we're gonna do is just momentarily suspend this.

  • Um, this this code routine.

  • So we're gonna wait a sink.

  • Eo, I gotta stop.

  • It was supposed to be a joke, and now I can't stop anyways.

  • A sink.

  • Io sleep 0.0 Let's just sleep for some amount of time.

  • Um, again, depending on your function and how many times this might be called, you would probably want to optimize this in a much better way, but this is just for example sake.

  • Um okay, so all we've done between running it where this hyung and now is just added this little bit.

  • So will this ever run on dibs?

  • Three.

  • No, it will never hit this A sink.

  • Yo, sleep a sink.

  • Io sleep.

  • It will never hear on this 1 50,000 It's gonna hit it twice, right?

  • Ah, and then on this one's gonna hit a bunch.

  • So So let's go ahead.

  • And, um, this is running a sink.

  • I Oh, hey, sink.

  • So, as you can see, it immediately ran, uh, did our Ah, it's interesting.

  • It's not doing We're not finishing 50,000.

  • The 152.

  • We immediately got a result for the 1st 1 We're kind of waiting on our 2nd 1 which is crazy.

  • I feel like we should have had that sleep done by now.

  • I don't know why we're still waiting on that.

  • I should run much, much quicker.

  • Ah, let me pregnant.

  • Well, first of all, this one's huge.

  • And if I recall right, that's a 36 2nd main justice real quick.

  • I think that one took 36 seconds.

  • Toe run.

  • Where's Mom?

  • Coffee.

  • Here's Ah, no three seconds.

  • So the other one, it's because of this stupid sleep.

  • Um, I'm just trying to show it for for example, sake.

  • Let's just revert back to 50 8000 and I'll stick with We could even do 500,000.

  • Just so this sleep on Lee gets hit one time, ever.

  • So there is going to be, Ah, trade off.

  • It's not like it's It's super seamless, toe like pause and then do some stuff.

  • You don't want to pause like a 1,000,000 times a second.

  • That's not wise, but it's still hanging.

  • Uh, what am I?

  • What do you think we're doing wrong here?

  • Sure.

  • Why?

  • Uh, return located.

  • I feel like it.

  • This something's gotta be wrong.

  • So enter life to bug mood.

  • So let's just do loop dot debug early.

  • See?

  • It's I think it's like loop.

  • That's set to bug.

  • Uh, stew.

  • Yeah.

  • Let's try looped on set.

  • Underscore er the bug toe one.

  • See if we can get anything from this thing.

  • 123123 We're waiting.

  • Yeah, even that responds.

  • Like that response shouldn't take that loan.

  • Um mmm.

  • If I modular.

  • Oh, my gosh.

  • Okay, Okay.

  • Who that's why.

  • Okay, anyways, uh, yeah.

  • So?

  • So that that's always true.

  • Wait to start again.

  • Cool.

  • All right, So what we did wrong or what I did wrong here is we never had the double Eagle zero.

  • So basically, it was just What's the remainder of I divided by 500,000 If that's not zero.

  • Congratulations.

  • That's a a true statement.

  • Uh, and then we sleep, so yeah.

  • Whoops.

  • OK, Back to back in action now.

  • Ah, yes.

  • We should only hit that one time.

  • So what if we made that 50,000?

  • You should see that.

  • This?

  • Yeah.

  • OK, so So, depending on which way you wanted to run this?

  • Um, yes, sir.

  • Which way you wanted to run this, uh, you like so like this is 50,000.

  • So it's going to sleep at 50 50,000 mark, like on this one.

  • If you use 50,000 it'll sleep twice.

  • Right?

  • But it'll never sleep on this one.

  • So it's It's fairly reasonable Thio to see that when we run this.

  • Yes, it ran largest middle, smallest.

  • And then it was like basically a ah, last in first out kind of situation where you had, uh And then it solves this one.

  • Then this one, this one.

  • But if we set this to be larger than 100,000 so if we use 500,000 that actually Really I'm not sure why that happened.

  • It was a typo.

  • Anyways, um, we have 30 to 10 and then basically this one, Then this one gets fed in.

  • Then this middle middle one gets solved in the smaller one gets also bet on which way you wanted your program toe act, you could you could change this.

  • And basically the funk that that there is that we just never await a asleep in the same pain A synchronous sleep.

  • So Okay, so at this point, we've basically the program is a synchronous We were kind of deciding whether we want to use 50,000 or 500,000 depending on which way we want to run.

  • It doesn't make all that big of a difference.

  • But as we've seen the sleep itself, even if it's a very, very small sleep, you're paying a slight penalty for the trade off that you're doing there.

  • Because again, this is all concurrent.

  • This is not parallel your You have to stop and switch tasks.

  • So just like when you stop your run to tie your shoe, that's a little slower than if you had just kept running.

  • You've got bending over time to tie your shoe, right?

  • Um so So just keep that in mind.

  • Now, um, I think at this point, we've basically covered everything we need to cover.

  • But there's a few other little things.

  • Um, actually, I think there's one more essential thing.

  • And that is how do we get the values from our, uh, our functions.

  • So, first of all, um, just right down here, we can return dibs.

  • One dudes to dibs three.

  • Now, uh, I'll just return him first, and then we'll talk s o Then once you've returned, these, um Basically, when you run this right here, this does return things that returns the's like result objects.

  • So we could say D one d two d three equals looped out.

  • Run until complete.

  • Then what we have are the result objects.

  • They're not results.

  • So you actually need to say, like, Let's say we want to print amount We'll just print the one dot results So we just run that result method basically on that And boom.

  • So four D one, which was here all the numbers divisible by 4 34,113 in that range.

  • Here they are.

  • So that's how you can get your results.

  • So next what I want to do is talk about what happens when we make mistakes.

  • So first of all, because there's a few mistakes that you're just simply going to make when you're writing a synchronous programming, I think there's a view, so but probably the biggest one is is not awaiting your your returns.

  • So what if we let me just comment out that requesting of a result here and then I'm going to comment out, uh, the await and the return.

  • Let's just say we're just gonna run these these things.

  • And in fact, let's leave the away.

  • Let's run it.

  • Okay?

  • And we could see that it works a synchronously.

  • It solves the easiest one first, and then the 2nd 1 and then 3rd 1 What if we comment out the await?

  • So now we're not awaiting anything.

  • What do you think's gonna happen?

  • Is it gonna run synchronously or is it or something else?

  • Right.

  • Um uh, do we have the But Okay, so this might be happening because of the bug.

  • Let me unset the bug.

  • Yeah.

  • So the bug through the heirs for us, Um, and then even now, since I'm running this in sublime, it's like the equivalent of running it like in a consul.

  • If we were to run this in, um, if we were to run this and in fact, it might even be the loop dot Close.

  • Let me.

  • I'm just curious.

  • I'm just curious to this point.

  • I actually don't know the answer to this one.

  • If I comment this out, will we get the warning that the task got destroyed?

  • No.

  • Nice.

  • Okay, so at this point, we can see what has happened.

  • Well, what has happened is we started all these co routines, but we can we can zoom through our script before we could get the return of the code routine because we just dispatched those out.

  • And they're often the They're just gone now and and, uh and so at this point, So I guess if you don't close your loop, So at least here, it would seem to suggest I just wonder if if this is complete.

  • So maybe that's a reason, though, why you should still have luke dot clothes even if you use run until complete.

  • Because what if you don't complete all right, If you don't complete well, you should still finally close the loop.

  • So because if you do finally close the loop, you would see, um, at least in consul form this.

  • But if you're running this like an idol or something like that, you would never even see this.

  • And this isn't breaking.

  • These aren't breaking errors.

  • They're just they're just like these.

  • Like warnings.

  • Basically, it's just telling you Hey, something happened.

  • But interestingly enough, if you do set the bug on and you run this, it actually errors out, it gives you an actual error, which I think is interesting.

  • Um, that just setting a debug will give you errors.

  • Like like it'll break it.

  • Um, So anyway, it's a useful thing to use the debug.

  • True, just because you're going to make mistakes like that.

  • So that's one of the mistakes.

  • And now what I'd like to go ahead and do is fix our code.

  • So let's go ahead and bring everything back, Okay?

  • So the other major mistake, um I think is just getting used to the syntax.

  • And if you start writing deaf, May it's like, What if we if we just remove a sink from in front of defined main Very suddenly you can already see Sublime is telling me, um, but if we go to run this boom, suddenly we have a syntax error, right it.

  • And at least if if your brand new to writing working with a sink, I oh, uh, you're gonna probably forget to do.

  • You're gonna fry, forget the A sink at least one or two times, and then you're gonna try to use this sort of logic here, Um, and then you're gonna be Oh, what was this in tax again?

  • And you're using the right syntax here.

  • You're just not using the right Syntex up here.

  • I actually kind of think I haven't really decided, but I feel like it should probably point back up to the function name or something or that what should be a code routine name.

  • Anyway, um, I think that's enough for a quick intro to a sink.

  • I owe a produce some more stuff on a sink.

  • Io maybe maybe do something a little more project based than just real basic like this.

  • This is not really super practical.

  • Um, but basically, the crux of the situation is it's all about a sink, a wait and sewn, sort of some sort of way to suspend things again.

  • Like I said before, I think it's a Iot.

  • Http, I need to open up a search bar a Iot, Http, which I'm pretty sure his third party it is.

  • Yes.

  • So it's for a sink.

  • I Oh, but it's an HDTV client server.

  • Basically, for eight, uh, paged for a sink.

  • I owe so really, really common task.

  • You were saying before for why you would want a synchronously do things Is anything with Webb Webb, right?

  • And And so if that's your goal with a single, so you might want to use a Iot Http?

  • Mostly because, um I think it's mostly just so you can have the asynchronous client personally because otherwise you you probably have, or at least a session and stuff like that.

  • So working that out and having everything be really, truly asynchronous would be a lot of work.

  • So anyways, um, yeah, check that out.

  • So anyway, I think that's all for now.

  • If you guys have questions, comments, concerns, whatever, feel free to leave them below.

  • If you've got something you want to add that I make a mistake, Whatever.

  • I'm pretty new to doing a sink io, especially in Python.

  • So I probably made a mistake somewhere in here.

  • So anyways, um, that's it for now.

what's going on, everybody.

字幕と単語

ワンタップで英和辞典検索 単語をクリックすると、意味が表示されます

A2 初級

Asyncio - コアーチンを使った非同期プログラミング - Pythonプログラミング中級編 p.26 (Asyncio - Asynchronous programming with coroutines - Intermediate Python Programming p.26)

  • 2 0
    林宜悉 に公開 2021 年 01 月 14 日
動画の中の単語