The Hard Parts of Servers & Node.js Timer Queue
Transcript from the "Timer Queue" Lesson
>> Will Sentance: The event loop is very strict. What rules does it set for what code to run next and when those functions are allowed back in. Lets get started, line 1, Rumen what are we doing, what are storing in our global memory.
>> Roman: Declaring a function that use imported tweet.
>> Will Sentance: Yep use imported tweets, inserting a function that use, imported tweets use imported tweets there it is store function. Excellent thank you Roman next lines Zep.
>> Zep: We store the function definition immediately.
>> Will Sentance: Yeah, there it is immediately. Next line, Sam?
>> Sam: Store the function definition print hello.
>> Will Sentance: Print hello, excellent. Next line, Virginia?
>> Virginia: I just throw the function definition black for 500 miliseconds.
>> Will Sentance: Fantastic. Thank you to Virginia.
>> Will Sentance: Perfect, four functions. You know we have to deal with complexity when I have four functions, because I try and minimize this. Okay, now we get started.
[00:01:34] And one of them is the ability to set up a timer. Now, exactly how timers are run in Node is using, well, what are timers? In general, timers are just a time in the computer clock and comparing a future time for how many milliseconds past to see whether or not it's gonna take the past around the thing which you delete by assign.
[00:01:56] So would of call of the time nobody is technically doing that in the background is setting of start time with that function was which it set time out, set up the timer. In the background, and then checking actually loop UV, and actually technically it's the event loop that checks every time, has that timer and not part time pass such that timer would want to run.
[00:02:43] Set time out passes first, inputs the first argument of set time. Said time out. By the way we find the docs of it on the node docs is a noted feature built in C++ with the help of LUV. Okay, so the second arguably policies number of milliseconds want a delay.
[00:03:01] Where did you say Zep, set time it's gonna have its consequence.
>> Zep: In node.
>> Will Sentance: In node, so set up in node. A timer with that function and that amount of milliseconds delayed, there it is, timer and zero millisecond delay. And what function to auto run when it's done, is it?
>> Zep: [INAUDIBLE]
>> Will Sentance: Print
>> Zep: Hello?
>> Will Sentance: Hello yes good it took us six hours for a British impression to happen, excellent thank you. All right now the function, I got a new little piece here called status. Status is if at this moment, is their activity or has it finish so zero let's say we are setting this as a roughly zero millisecond, at zero millisecond.
[00:03:56] Zero millisecond is this timer completes, so zero millisecond timer it's you complete.
>> Sam: Yes.
>> Will Sentance: Yeah. So what's gonna be triggered to run?
>> Sam: Print hello.
>> Will Sentance: Print hello, do you think it's gonna be out on the cool stock? Seems reasonable, do you think it is, no. Anything, don't say of course not except obviously border line rational, and nah, it's not.
[00:04:44] So where instead, it doesn't just sit there though, where instead does it go? It goes to our first queue. The timer queue. Interesting, goes into our first queue the timer queue. And at zero milliseconds roughly,
>> Will Sentance: We get the function. Print hello into the timer queue. So all the set timeout ever does is tell you the time it will take before the function gets put in the queue, not when it's gonna get run.
>> Will Sentance: Good, we don't really set ourselves up for that nice. Next one we're back into our good old familiar node built-ins.
[00:05:44] Now, technically of course we have to add in FS using require that we'll grab and give us the access to nodes FS feature where we're gonna use it. So here we go. I'm just using these numbers here as ordinal. That means the order that matters. It's definitely not a millisecond later.
[00:06:01] You've got a nanosecond later. But I just want to show you time progressing one millisecond later. We're going to run fs.readfile and pass it. The two important things we need to know where the file is known as a path. And what function to auto run, there' a call back function, what function we want to have node automatically run for us.
[00:06:30] Fs.readfile. What does it do in Note? Michael. Sets up an instance of the FS feature of Note.
>> Michael: Yes, to access the file system.
>> Will Sentance: Absolutely, with the help of LibUV, and actually sending a background thread to handle the passing of the data and focus on the data coming in.
[00:06:57] Yeah, it needs to know what path from a dot means the current folder in which her running Node,we always had to run node in an existing folder. We run it in the same folder as we say the tweet. So dot means that slash means do in this folder.
[00:07:11] Go and find tweet stop. Jason. Okay, at one millisecond Is it done. Now it's going to take a few milliseconds to get that data out. So it's not done right now. But when it is done, what function is going to auto run Andrew?
>> Andrew: Using party tweets.
>> Will Sentance: Fantastic.
[00:07:33] Exactly. We know the URL and we know the function is going to auto run. It's gonna run US imported tweets. There it is. I'm just gonna separate this out so it's clear.
>> Will Sentance: Here we go. So, we are now using the FS feature and the timer feature in the background.
[00:07:55] Two auto run functions set up and ready to go already. Okay, the data is gonna start coming through the background thread's handling the data assigned to come through, but at one millisecond, it's definitely not ready. We definitely have the tweets ready to go. We're definitely not auto-triggering using for the tweets, not yet.
[00:08:41] Lock for 500 milliseconds. We're not going to describe how it works. We just know that as the thread enters it, it gets added to what? Sam?
>> Sam: Cool stack.
>> Will Sentance: Cool stack, whose side is pushed to the cool stack. Cool for 500 milliseconds, my energy so hit I was it wasn't a question.
[00:09:02] It was me trying to convince myself. Why are you still here?
>> Speaker 8: [LAUGH]
>> Will Sentance: I'm getting similar questions but, okay, good, I like it, that's actually very inspiring to me, thank you, Michael. There it is, I head into it, it's gonna sit, therefore last 500 milliseconds, how? Not by setting up timers down here, nothing to do with timers.
[00:09:35] So any long term titles, it slowed like a timer is auto delegated to the background. The only way to trick it into taking a long time or blocking the thread from running further is to do a task like, lots, lots of tiny tasks like accessing an array, adding an element.
[00:10:09] But that was absolutely unused in practice we do not block. The only way we can do it typically is by doing something silly like a very big or silly. But you know like finding an enterprise number or. That would block and that's what we're doing.