Placeholder Image

字幕表 動画を再生する

  • so right.

  • Worker threats, Crash course.

  • All right, let's see.

  • Is this work the way I want it to?

  • A kind of does.

  • Um, yeah.

  • So I'm kind of stuck with the title because I chose it, But it's real.

  • This is really more like an introduction to the things I like.

  • Slash use slash.

  • Want to talk about work or threats or perhaps even more accurately.

  • I was doing a thing, and I wanted to talk about the thing and the end of talking about worker thread so that I could stand up and talk about the thing.

  • Um, so I talk fast.

  • I usually say, I will try not to, and I will fail and I'm sorry, but I have 25 minutes and, like 40 billion slides, So talking fast is by superpower Right now.

  • I know it's not very Hawaii to talk really fast, but doing the best I can.

  • Anyway.

  • My name is Rich and I work for the universe of California San Francisco campus in the Library B and then include I'm including myself in this.

  • We all act like the Internet didn't exist until Amazon and pets dot com came along.

  • But government institutions, including public universities like Universal California were there for decades working on hard problems like Effin Ethernet.

  • How does it work?

  • One of the jazz concert Why organizers had a block post about the role of the University of Hawaii in Ah, and that sort of stuff.

  • And you should read it because it's really fascinating.

  • Anyway, As you might imagine, the UCSF Library does not have an extraordinary large Deverell budget.

  • Um, don't beloved several positions.

  • There's not a lot of money around me to travel to tropical locales to attend conferences and talk about JavaScript.

  • So for making it possible for people like me and, perhaps more importantly, people who are not like me in several respects to be on the stage of this conference.

  • Mad props to the Jazz, Conway Organizer's and the sponsors.

  • Uh, so I am the composed of a rock opera.

  • This would normally be by fun fact, by the way, but since it was part of my talk, I I held it back.

  • I'm the capos of a rock opera about a steakhouse and includes dancers and steak suits and things like that.

  • Uh um, it sounds, I hope this works.

  • It sounds something like this.

  • Okay, it's gonna hang on.

  • Wait.

  • It's really, really groovy.

  • So anyway, um, but, uh, we're gonna That's actually the good thing to fail, because didn't actually come here.

  • Thio play excerpts from from a rock opera.

  • I just bring it up because I put most of most of the like I put a link to the slides and the link to all the things I talk about in here.

  • Uh, palace family steakhouse dot com.

  • Um, so now the only reason I get to stand up here and play except for my rock operas because I work on note, by the way, who runs knowed Everybody does.

  • If you if you don't think you did, you run note, right?

  • Okay, so if you're using this, it doesn't matter.

  • I only talk five minutes.

  • Um, um uh, if you're running noted, you're running a lot of code that I wrote on a 2013 Mac book.

  • Just pointed out.

  • So So, um, so, uh, me And like, 200 other 2 2000 other people.

  • But anyway, so it's a quick disclaimers.

  • The views expressed my own.

  • Not so Those in my employer.

  • That's a standard disclaimer.

  • Also, the views expressed on my own and not necessarily those of no did.

  • There are a lot of other people involved in note, and naturally, I want to see everything the same way.

  • Just two days ago, was decided to remove a handful of deprecate id AP eyes from the code base that I think we should just leave in there.

  • So clearly my opinions are not those of note necessarily.

  • But I wasn't nearly as angry as this person was about it anyway, with out of the way back to note.

  • Hey, have you heard about worker threats?

  • Um, they were introduced in no 10.5 dot zero, but it required a command line flag to using that version.

  • So in No.

  • 12 you don't need a command line flag, and you really want to use 12 11 or newer because that's the first version where worker threads were determined to be stable rather than experimental.

  • And really, you want to use 12 15 0 because that's the version that was released like a day or two ago that fixes some security issues.

  • If you like to live on the edge, use the current version that that won't be supported yet in a few months.

  • But it's No.

  • 13 80 which is the version that was released a few days ago.

  • Security fixes anyway.

  • Yeah, worker threads.

  • What are they?

  • Even so, they're kind of like Web workers, but different.

  • There's no there's no shared worker, For example.

  • Uh oh, that's there.

  • It is Okay if he was not used Web workers, don't worry, Stay with me.

  • They're also kind of like threads and other programming languages, but also not like threads and other programming languages.

  • If you use threats and other programming languages, cool.

  • If not, don't worry.

  • Stay with me, man.

  • That image takes a wild blue.

  • It's pretty big.

  • Okay, anyway, Java script is single threated, as we all know, even if you don't know what that means.

  • There's an excellent chance you've heard Java script a single threated before seeing it on this slide.

  • That's because there's one cold, hard, unchanging fact in this ever changing world in which we live in.

  • And that unquestioning fact is JavaScript is single, threaded, arguably literally arguably because, like some people like to argue about this, I don't want to have that argument, though.

  • So the point is, your code does one thing at one time.

  • Uh, it's why this program never exits.

  • There's only one execution.

  • Uh, I'm sorry.

  • That's why there's only, uh, who's on one execution threat handling this.

  • Could the code in the set time out, which would cause the wild loop to stop, never executes.

  • Right.

  • Um, it's, you know, it's just going to sit in the wild loop forever and and and the time out one fire until the wild loop exits the while.

  • It's never gonna exit.

  • So the time that never fires stuck in flew forever.

  • Classic deadlock.

  • Right.

  • So this code will run forever, or until you press control, see, or you turn off your computer or whatever.

  • But it's not gonna exit cleanly and whatever causes to exit, it's not gonna be the set time out.

  • This is called blocking the Event loop.

  • You may have heard that phrase before you heard it in Max's talk.

  • Like 20 minutes ago.

  • He recommended Sam Roberts video.

  • That's great.

  • It's great.

  • Great talk.

  • I recommend this one.

  • This is ah, What the heck is the event, Luke Anyway by Philip Roberts.

  • If you get nothing else out of this talk.

  • Let it be that you should get more than one thing out of this talk.

  • But if you could only get two things from this talk let the 2nd 1 be that you want to watch this talk, okay, that talk, not this talk.

  • Since what happens when I have to compress four hours worth of material in 25 minutes?

  • Anyway, there's a link to that talk of Palestinians taking dot com.

  • Now you may be thinking, but note is a synchronous.

  • I don't really have to worry about blocking the event.

  • Look, you're probably not thinking that, but you might be.

  • Someone might be it.

  • Can you?

  • No.

  • Can effectively do many things at once.

  • Like handle multiple simultaneous http requests or read multiple files.

  • Max was just telling about telling us about this in the last talk, and his slides were gorgeous.

  • So it must be true.

  • And it is true because Max is a good egg.

  • He would not lead you astray.

  • But the built in a synchronous nature of node has been around input output.

  • I Oh, that's why I mentioned files.

  • And that's why I mentioned TV.

  • If you're doing, say data science stuff, maybe, Or, um uh, like processing graphics, you know, image, image processing or basically, anything that CPU intensive.

  • Then let's just say that the default state of things in node and in javascript eyes not as a synchronous so prior to worker threads the usual way people would offload CPU in a non blocking way.

  • In note was the cluster module.

  • And if that's working for you, great.

  • But here's the thing.

  • Cluster spreads your workout workload out across multiple processes, and each of those processes has independent memory and so on.

  • So I make sharing large amounts of data problematic, and each process consumes the full amount of RAM required.

  • Bye note.

  • So this could be really inefficient again, though, if it's working for you, great.

  • But if it doesn't work for you, if it doesn't work for a lot of things, and even if it is working, worker threats will often work out better.

  • And here's why.

  • Worker threats are my weight, not a whole process.

  • It's just a thread, Um, and they're better at sharing data, which will talk about briefly and then I'll do some hand waving stuff until you go look at the documentation.

  • Um, so let's dive in.

  • Uh, here's a hello world example on.

  • Let's just go through it step by step.

  • The first line pulls in three things from the worker threads module.

  • It pulls in the er year starting from the right hand side.

  • Suppose in apparent porter pulls in the main red Julian and it also pulls in the worker class And, um, yeah, so is made thread at the parent port.

  • So we use is made thread thio to make sure we are We're not using.

  • We're not inside a worker threat already.

  • Basically, we're checking, you know, we're checking that we're not aware of threats so that we know that's okay to launch a worker threat.

  • If we didn't do that, we might be in a worker threat that launches a worker threat that launches a worker threat that launches work threat and just working threats all the way down until you're run out of, I don't know, stack heap something, some kind of ramp.

  • Um, when you're in some kind of resource, uh, this check is usually only necessary if you have your worker thread code and your main threat code in the same file.

  • I don't usually like to do that, but for a hello world example, it seemed convenient to just have one blob of code and not like.

  • Here's the word.

  • So that's what it is.

  • Is main threat.

  • Cool anyway, eso we're in the main threat.

  • So what we're gonna do is create a worker threat.

  • So we use the constructor for the worker class, and that's what that new worker is all about.

  • Right?

  • New is the key word for Instructor's job script, and we pass it under bar under our file name, which is the special no Jets variable that says, uh, you know the file, the path to the file that contains the code currently being executed.

  • If you didn't know that, that was what that was.

  • Now you do.

  • You can create a worker threat to run any job script spot while you specify.

  • But like I said here, we're just keeping it 15 You can also pass it a string that and and tell it that this is a blob of code to execute.

  • I don't like to do that because that's basically Val, and, you know, we've all been taught that Val is, you know?

  • Yeah.

  • God kills a kid or something like that when you fell.

  • You know, um, you and so you know, like to avoid it, But, um Anyway, uh, so we've created a worker.

  • Um, And now it's Listen for messages from the worker.

  • Uh, this is the usual event listener, syntax and note.

  • Remember, we're in the main threat.

  • Still not the worker threat.

  • We're listening for messages, message events on the worker we've created.

  • And when we get one, we're gonna use Consul Log to print the message.

  • Okay, so that's it for the main threat.

  • That's all we're doing now, remember, we were in an f block that checked if we were in the main thread.

  • So now let's use the else block and do the right stuff for when we're in the worker threats.

  • And all we're gonna do is use parent port to send a message to the main threat in the main threat.

  • Parent port will be No.

  • But, uh, you know.

  • So if you want to send a message to the worker from the parent point, we use the port.

  • The post message method that's on the worker.

  • Instance we'll see that later.

  • Sorry, I'm talking so fast, but in a word with red parent port, that post message.

  • Like we see, they're used to send messages to the main threat.

  • So let's use it to send a message that says Hello World.

  • And that's the end of file.

  • You'll remember them.

  • The main thread.

  • We set up a listener that would take the message and print it out.

  • So this is the convoluted way to console Log Hello World.

  • Not terribly useful.

  • There are much easier ways to do that, of course, but it doesn't you just the very basic concepts of worker threats.

  • And now let's do something equally contrived but more interesting.

  • And I'll try to slow down a little six degrees of Kevin Bacon.

  • Perhaps you remember the game.

  • Uh, if not, it's simple, given the name of an actor in the film your job is to.

  • That's a cool effect.

  • Your job, should you choose to accept it, is to connect to them to Kevin Bacon in six or fewer steps in the following manner.

  • Let's say your challenge to connect Katy Perry to Kevin Bacon.

  • I don't think this is gonna get old in six or fewer steps.

  • Uh, Katy Perry words Katy Perry wasn't Zoo Lander to with John Malkovich, and John Malkovich was in Queens Logic with Kevin Bacon Boom, Katy Perry to Kevin Bacon and Two Steps.

  • I've seen neither of those films.

  • Um, anyway, there are already websites that solve six degrees of Kevin Bacon by using IMDB data Several years ago, I wanted to do this for musicians playing on recordings of individual songs, so I made a site called Music Roots, and it's been broken for a long time because some one who I want say, what who was but removed in a P I that I was dependent on.

  • And then everybody, they're fixing it anyway.

  • It's been brewing for a long time, so let's fix it So First.

  • Surprisingly, there is no usable database available on what musicians play on what tracks on what recordings.

  • And some people say, What about all music?

  • They only have information.

  • Who played on what album not track by track information.

  • Other people ask me about music brains.

  • They only have artists associate with tracks, not individual musicians.

  • So middle class about discards.

  • They have published album credits, but Sometimes the credits don't list track by track data, and often they are wrong, even intentionally.

  • So asked me for examples after the talk, because I love talking about this sort of stuff, but otherwise, just know that disguise data correctly reflectors on an album sleeve, which does not correctly reflect what is in reality all the time.

  • That brings us to a wicky data, which has some data all along these lines, but way less than you think.

  • That's cool, though.

  • It's wicky data so everyone can add data to it.

  • But it's very, very unusable.

  • E slow for the many, many, many queries that would be required to do this.

  • So I built my own database and published it.

  • It's very incomplete, but it will do here.

  • Uh, I also built a rudimentary little visualize er for it, and we'll see that in a bit.

  • Uh, so Okay, in order to solve these things, we could use breath first search, and many of you, maybe most of you, maybe all of you, I don't know may already know what breath for searches.

  • But just in case, I am now going to give the world's worst overview of breath for a search.

  • Let's go back to connected.

  • Katy Perry to Kevin Bacon.

  • Step one is Katy Perry.

  • Kevin Bacon.

  • That's a job triple equal there in the middle.

  • The answer is obviously no Step two.

  • Find everyone that was in a movie with Katy Perry.

  • Do any of those people having to be Kevin Bacon?

  • And the answer is still no Step three.