TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

A guide to threads in Node.js

165 pointsby bibyteabout 6 years ago

7 comments

tannhaeuserabout 6 years ago
&gt; <i>It was only in 2009 that Ryan Dahl, creator of Node.js, made it possible for developers to use the language to write back-end code</i><p>Server-side JavaScript was a thing <i>long</i> before that. In fact, Netscape used it as early as 1994 [1], predating Java as a backend language. And CommonJS (on what nodejs&#x27; modules and many APIs are based) was a community effort towards a common API by 2000s SSJS implementations (helma, v8cgi&#x2F;teajs, and others).<p>Apart from that, a nice read for those who need a WebWorker-like API for CPU-bound nodejs tasks.<p>[1]: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Server-side_scripting" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Server-side_scripting</a><p>[2]: <a href="http:&#x2F;&#x2F;wiki.commonjs.org&#x2F;wiki&#x2F;CommonJS" rel="nofollow">http:&#x2F;&#x2F;wiki.commonjs.org&#x2F;wiki&#x2F;CommonJS</a>
评论 #19478631 未加载
评论 #19481569 未加载
评论 #19478731 未加载
评论 #19478135 未加载
评论 #19480271 未加载
评论 #19478912 未加载
crabasaabout 6 years ago
<i>&gt; type WorkerCallback = (err: any, result?: any) =&gt; any;<p>export function runWorker(path: string, cb: WorkerCallback, workerData: object | null = null) { const worker = new Worker(path, { workerData });<p>worker.on(&#x27;message&#x27;, cb.bind(null, null)); worker.on(&#x27;error&#x27;, cb);<p>worker.on(&#x27;exit&#x27;, (exitCode) =&gt; { if (exitCode === 0) { return null; }<p><pre><code> return cb(new Error(`Worker has stopped with code ${exitCode}`)); }); return worker;</code></pre> }</i><p>For an article titled &quot;A Guide to Threads in Node.js&quot;, I really wish the first example of writing a thread wasn&#x27;t in TypeScript.
评论 #19478584 未加载
com2kidabout 6 years ago
I wasn&#x27;t a huge fan of the &quot;single thread for everything&quot; paradigm, then I helped design a single threaded cooperatively multitasked embedded OS in C&#x2F;C++.<p>No overhead from context switches. Everyone who is running knows how long they have to run, and knows that they are NOT going to get interrupted. No need to worry about locks or how to share data. Want to pass data to another module? Just pass it through well defined interfaces and you darn well know there will never be a read&#x2F;write conflict, and the next time that module&#x27;s code runs, it&#x27;ll have access to that data. (No queue!)<p>The only exception, and what made it all possible, was the interrupt routines from hardware[1]. Anyone who subscribed to hardware events (entire OS was subscription based, no polling reads ever) had to implement a &quot;thunk pattern&quot; to put data into a receive buffer. The design pattern to do this was the same everywhere in all modules, making code understandable across the entire project.<p>It is an incredibly freeing paradigm to write in. It becomes so much easier to prove[2] the correctness of code when you can read all the code straight through and not have to ever worry about someone stomping on your data.<p>It wasn&#x27;t an RTOS, but even so, it becomes really easy to start providing performance guarantees.<p>Internal builds had a watchdog timer[3] that would crash the device if it wasn&#x27;t &#x27;kicked&#x27; every so often. Set that to 3ms, start working with the code, and the stack traces tell you instantly who is over their CPU budget. Rewrite code and break it apart into multiple chunks that are scheduled for later execution, repeat until everyone is under their CPU allotment.<p>For many tasks, single threaded code is <i>nice</i>. Getting rid of preemption is even nicer.<p>Preemptive multithreading is a compromise. It means that no one thread&#x2F;process can bring down the system by hogging 100% of resources, but it also creates a huge overhead where important work, work that makes for a better user experience, can (will!) get interrupted for work that honestly doesn&#x27;t need to be done right now.<p>The solution to this is just throw so much CPU at the problem that everything gets done in a reasonable amount of time. It has often been noted that &quot;reasonable amount of time&quot; means systems today are less responsive than a 486 running DOS from 1992.<p>Of course it isn&#x27;t reasonable to have a modern cooperatively multithreaded consumer OS, no way would the hundreds of processes ran at any one time all cooperate with each other.<p>But if you ever get a chance to write code on a single threaded cooperative system, go do it. It is a lot of fun.<p>Now all this meant that going from embedded C to NodeJS wasn&#x27;t that large of a mental leap! Not having to directly read bytes off the wire was weird (seriously, took a bit of getting use to), but it turns out that &quot;get data, do work, schedule what needs to be done, return early&quot; ends up being the same paradigm at both the top and the bottom of the programming stacks!<p>[1] All I&#x2F;O was done using DMA engines, basically a fancy limited programmable piece of hardware that can read and write to all the different pieces of HW hanging off of the main chip, so for example as Bluetooth packets come in, the DMA engine shoves the packets into a buffer and when the buffer is full it raises an interrupt that lets the CPU know that data is waiting. It looks almost exactly like async I&#x2F;O in any of the modern programming languages, except you have direct access to all that IO being <i>hardware offloaded</i>. Writing to the bare metal rocks.<p>[2] For a reasonable enough degree of &quot;prove&quot; that software is reliable and doesn&#x27;t crash from threading issues<p>[3] A watchdog timer is a physical timer hooked across the power lines of your chip. If it isn&#x27;t activated every so often (in the industry this is called &quot;kicking the watchdog&quot;) it will, in debug builds it does a controlled crash of the CPU, and in retail builds it will reset the entire system. If you&#x27;ve ever had an embedded device reset itself before your very eyes, it is becomes the CPU got locked up and no one kicked the watchdog, so the entire system emergency reset itself.
评论 #19478615 未加载
评论 #19478033 未加载
评论 #19481582 未加载
评论 #19481315 未加载
评论 #19481324 未加载
Trisellabout 6 years ago
Honest question. Are we going to be able to pass functions with data to threads? Or are these only designed to use files? Which makes this feel like a better wrapper on the child process api that already exists.
评论 #19478050 未加载
评论 #19479228 未加载
Scarbuttabout 6 years ago
With the javascript community fixing most of it shortcomings, I suspect in a few years JS&#x2F;TS is going to be way far ahead of any programming language in term of users.
评论 #19479237 未加载
sbhnabout 6 years ago
A good video on parallel processing CPU bound taks with nodejs. <a href="https:&#x2F;&#x2F;youtu.be&#x2F;ZYfSe9qKaZE" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;ZYfSe9qKaZE</a>
dborehamabout 6 years ago
Haven&#x27;t read the article but I&#x27;m assuming it&#x27;s an empty page?<p>&lt;ducks&gt;
评论 #19477904 未加载
评论 #19478635 未加载