12/30/2023 0 Comments Nodejs event loop![]() ![]() The loop checks the cached now to have active callbacks that expired execute. This is where setTimeout or setInterval callbacks run. As shown, pending callbacks in the queue keep the loop alive. Is the loop alive? If the loop has active handles, active requests, or closing handles, it’s alive.These system calls are internal to libuv. The event loop caches current time at the start of the loop to avoid frequent time-related system calls. Feel free to follow along in a REPL or download the source code. It makes effective use of connections and threads while remaining scalable.Įnough theory time to see what this looks like in code. A single-threaded loop cuts thread-safety edge cases and can context switch much faster. The event loop crushes bugs when multiple threads share resources, because it’s single-threaded. Context switching between threads is slow, because it must synchronize current state like the call stack or local variables. If a callback comes back with data, it must marshal context back to the executing thread. Multiple threads in general also complicate the story. Threads are operating system resources that spin up, and this doesn’t scale to thousands of active connections. You may be wondering, why does the event loop execute in a single thread? Threads are relatively heavy in memory for the data it needs per connection. Node uses libuv under the covers to poll the operating system for callbacks from incoming connections. Think of the call stack as synchronous code that unwinds, like console.log, before the loop polls for more work. The loop itself is semi-infinite, meaning if the call stack or the callback queue are empty it can exit the loop. The loop doesn’t wait for everything from one request to complete, but picks up callbacks as they come without blocking. Multiple requests can get queued at the same time, which makes it concurrent. Because the loop doesn’t block, it’s free to work on more than one web request at a time. In the event loop, the main loop unwinds the call stack and doesn’t wait on callbacks. Instead of waiting on the database to respond, it continues to pick up other tasks in the queue. A single thread can only do one thing at a time. For those without a computer science degree, imagine a web request that does a database lookup. The event loop is a single-threaded, non-blocking, and asynchronously concurrent loop. One of the “killer app” features in Node is this loop, because it solved a hard problem in a radical new way. I’ll explore every part of the Node.js event loop and demonstrate how it works. In this take, I’ll show you how Node deals with this quagmire via the event loop. Both the operating system and the programmer must do a lot of work to deliver a solution that has many edge cases. Spinning up new threads and managing context switch in between is expensive. JavaScript is single-threaded, but does this limit Node from utilizing modern architecture? One of the biggest challenges is dealing with multiple threads because of its inherent complexity. ![]() This puts pressure on the developer to write efficient code that takes advantage of the hardware. There’s a thermal limit in each single core of the CPU, and nothing is getting any faster. The problem is inescapable because modern computing has multiple cores. Code that executes asynchronously is unpredictable and difficult to trace when there are bugs. Concepts like concurrency, parallelism, and deadlocks make even the most seasoned engineers shiver. Asynchrony in any programming language is hard.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |