Quiz
- Can you explain more about thread-based networking and node.js?
Sure, though this will be a significant digression.
Let's start by talking about concurrency.
- Suppose you are doing some ML work on the CS server, and it'll take 2 minutes of computation time.
- What are other folks doing? Waiting? No. The server gives the illusion of doing multiple things at a time by (1) timer interrupts, and (2) multiple processes handled by priority and round-robin.
- Suppose the server has multiple cores (which it does. It has 24 3GHz Intel Xeon Gold processors). In that case, it can do actual parallelism, but it still does timer interrupts and multiple processes.
But processes are relatively heavy. Lots of security barriers between them. Context switching is expensive.
Lighter weight are threads.
I discuss processes and threads in the fall version of CS 304.
However, even threads have overhead and synchronization issues.
For problems that are I/O - bound (rather that compute-bound), a better concurrency technique is an event loop. That's what browsers do and what Node.js does.
Both also have the ability to create extra threads, but that's not what we are discussing today.
- what does it mean that Node.js is designed without threads?
It means that there's a single thread that does all the computation, rather than spinning off multiple threads and doing concurrency by some kind of timer-interrupt and scheduling.
As a side-effect, it means that if some piece of computation takes N seconds, then everything else has to wait. So, it's better if the computation is short and not compute-bound.
- When exactly does the promise get executed? For example, if a user is continuously scrolling and clicking on a page, does the promise just never get fulfilled?
The promise will eventually be fullfilled, even if other things are going on. Say it's a network request. Even if the user is scrolling and clicking like mad, eventually there will be a moment when the event handler can get into the event queue and get executed.
But if the alternative is not I/O but continuous computation, the event handler may have to wait...
- What are the cases when using promises and callbacks as opposed to async await is advantageous? / When would it be appropriate to use a promise or callback over a async/await?
One good example is suppose I want to launch N network requests. If I use
await, they will launch sequentially. If I use promises, I can launch them simultaneously, wait for N responses (usingpromise.all) and attach athen(callback)to thepromise.all.But that's not what I'm expecting at this point in the course, or possibly ever.
- With
.then(handleFulfilled, handleRejected) .catch(func)
What is the difference between handleRejected and func?Not a lot. They differ if there's a chain of promises. The
handleRejectedis associated just with that one promise, while.catch(func)will execute if any promise in the chain is rejected.Modern practice is to use the more comprehensive
.catch()technique.According to ChatGPT, "The Promise spec designers themselves have said that the second argument to
.then()is mostly historical." - Can you run through the code on the blocking and nonblocking website?
for sure! blocking versus non-blocking
- does a non-blocking call always need an error catching statement?
Professional level code should always consider what happens if things fail.
We should as well, but we are beginners, and so let's focus on the expected, rather than the exceptional.
- if these questions are too much/not relevant enough to answer in class, I have an office hours meeting on Wednesday!! thank you :)
Thanks for that; I'll do my best right now, and you can always follow up later.
- what are child processes that can be spawned?
You can build things in the event loop that can notice a computationally heavy task (another ML training task) and create a new process/thread to work on it, in the timer-interrupt, scheduling idea.
So, the child process does the computation and lets the event loop know when it's done.
- what are sockets and how do they enable load balancing?
Sockets are basically open communications connection to another process, either on this machine or another. If they are to another machine, you can send some of your work over to it, balancing the load between two servers.
- these following questions are probably less relevant for the whole class, but I'm not sure I understand the following paragraph from the about Node.js page:
Node.js being designed without threads doesn't mean you can't take advantage of multiple cores in your environment. Child processes can be spawned by using our child_process.fork() API, and are designed to be easy to communicate with. Built upon that same interface is the cluster module, which allows you to share sockets between processes to enable load balancing over your cores.
See above. Node.js was designed for I/O-bound problems, and web applications and database applications are strongly I/O-bound, so it's a great match.
However, they are saying that you don't have to give up the other world; you can still do processes and threads if you want.
- all of them...
I'm happy to answer questions!