In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how Node.js uses worker_threads multithreading for parallel processing. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Many people don't seem to understand how single-threaded NodeJS competes with multithreaded backends.
To find out why, we must understand the true meaning of Nodejs as a single thread.
JavaScript itself was originally created to do simple things, such as validating forms, responding, and so on, and it wasn't until 2009 that Ryan Dahl, the creator of Node.js, made it possible to write server-side code in JavaScript.
Server-side languages that support multithreading have a variety of structures and constructs for synchronizing between threads and other thread-oriented features.
Supporting these things means that JavaScript needs to change the entire language, which goes against the ideas of the creators of javascript. Therefore, in order for pure JavaScript to support multithreading, Dahl must create a solution. Let's have a look.
How does Node.js work?
Node.js uses two types of threads: the main thread processed by the event loop and several worker threads in the worker thread pool.
The mechanism by which the event loop Node.js handles non-blocking Ithumb O operations-- although JavaScript is single-threaded-- transfers operations to the system kernel when possible. When the JavaScript operation blocks the thread, the event loop is also blocked.
A work pool is an execution model that generates and processes separate threads, then executes tasks synchronously and returns the results to the event loop. The event then iterates using the result to execute the callback provided.
Basically, the workpool handles asynchronous Istroke O operations-- mainly interaction with system disks and networks. Some modules use out-of-the-box workpools, such as fs (I/O-heavy) or crypto (CPU-heavy). The work pool is implemented in libuv, which causes a slight delay but is almost negligible when Node needs to transfer data internally between JavaScript and C++.
After understanding the meaning of event loops and workpools, let's take a look at the following code:
In the above code, we don't have to synchronize wait events. We delegate the task of reading the file to the workpool and use the result to call the function provided. Because the work pool has its own thread, the event loop can continue to execute normally while reading the file.
Let me introduce you to worker_threads.
With the release of Node.js 10.5.0, worker_threads appeared. It supports the creation of simple multithreaded applications in JavaScript
Worker_threads is a package of nodejs modules. A thread worker is a piece of code that is generated in a separate thread (usually taken from a file).
It is important to note that the terms thread worker, worker, and thread are often used interchangeably. They all refer to the same thing.
Worker threads in Node.js are useful for performing heavy JavaScript tasks. With the help of threads, Worker can easily run JavaScript code in parallel, making it faster and more efficient. We can accomplish heavy tasks without interfering with the main thread.
Worker threads were not introduced in older versions of Node.js. So first update your Node.js to get started.
Now create two files to implement the thread, as follows:
File name: worker.js
Const {workerData, parentPort} = require ('worker_threads'); console.log (`Write-up on how ${workerData} wants to chill with the big boys`); parentPort.postMessage ({filename: workerData, status:' Done'})
File name: index.js
Const {Worker} = require ('worker_threads'); const runSerice = (workerData) = > {return new Promise ((resolve, reject) = > {const worker = new Worker ('. / worker.js', {workerData}); worker.on ('message', resolve); worker.on (' error', reject); worker.on ('exit', (code) = > {if (code! = 0) reject (new Error (`Worker Thread stopped with exit code ${code} `)) });});}; const run = async () = > {const result = await runSerice ('Tunde Ednut'); console.log (result);}; run (). Catch ((err) = > console.error (err)
Output:
Thank you for reading! On "Node.js how to use worker_threads multithreading for parallel processing" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.