nodejs Mistakes

Thread

A thread is the smallest unit of an operating system capable of operation scheduling, we need to know the thread is part of the process, it is being included in the process. A thread can only belong to one process, but a process can have multiple threads .

Single-threaded

A single-threaded process is only open a thread

Javascript is part of a single-threaded program execution order (here aside JS asynchronous), imagine the queue, after completing an execution in front of, behind only be performed when you are using a single-threaded coding language Do not have too much time consuming synchronous operation, otherwise the thread will cause obstruction, leading to the subsequent response can not be processed. If you encode using Javascript, please use characteristics Javascript asynchronous operation as much as possible.

 

Some single-threaded Description

  • While Node.js is single-threaded model, but based on event-driven, asynchronous non-blocking mode, can be used in high concurrency scenarios, avoiding the thread creation, context switching overhead resources generated between the threads.

  • When your project requires a lot of computing, CPU time-consuming operation, pay attention to consider open multiple processes to complete.

  • Node.js development process, the error will cause the entire application exits, the application of the test of the robustness of worth, especially wrong exception is thrown, and the guardian of the process must be done.

  • Can not use single-threaded multi-core CPU, but later offered Node.js API as well as some third-party tools have been resolved accordingly, it will be mentioned later in the article.

 
child_process module and the cluster module summary
child_process module for multi-process, while cluster module to achieve the main thread to create a master-slave mode, and cluster.fork method to create a child process

 

Node.js thread

Node.js errors on single-threaded

  1. const http = require('http');

  2.  

  3. const server = http.createServer();

  4. server.listen(3000,()=>{

  5. process.title='程序员成长指北测试进程';

  6. console.log('进程id',process.pid)

  7. })

Still see this article first piece of code, created http service, opened a process, said the Node.js is single-threaded, so the number of threads should start after Node 1, but why it will open seven threads? Do not single-threaded Javascript little friends do not know there is no such doubt?

Explain the reasons:

Node in the core is v8 engine, after the Node starts, it will create an instance of v8, this example is multi-threaded.

  • The main thread: compile, execute code.

  • Compile / optimize threads: When the main thread of execution may be optimized code.

  • Parser threads: recording and analyzing the code running time, provide the basis for Crankshaft optimize code execution.

  • Garbage collection of a few threads.

So we often say that Node is a single-threaded refers to the execution of JavaScript is single-threaded (developers to write code to run in single-threaded environment), but Javascript host environment, whether Node or are multi-threaded browser because libuv thread pool concept has existed, libuv will simulate an asynchronous call to a different operating system by implementing a similar thread pool, which the developers are not visible.

Asynchronous IO will take some additional threads

Or the example above, we performed a timer at the same time, to read a file:

  1. const fs = require('fs')

  2. setInterval(() => {

  3. console.log(new Date().getTime())

  4. }, 3000)

  5.  

  6. fs.readFile('./index.html', () => {})

The number of threads into a 11, this is because there are some IO operations (DNS, FS) in the Node and some CPU-intensive computing (Zlib, Crypto) enabled Node thread pool and thread pool The default size is 4, because the thread number became 11. We can manually change the default size of the thread pool:

  1. process.env.UV_THREADPOOL_SIZE = 64

Easy to thread a line of code becomes 71.

Libuv

Libuv is a cross-platform asynchronous IO library, which combines the properties of IOCP libev under Windows and under UNIX, first developed by the author Node, specializing in multi-asynchronous IO support platform for the Node. Libuv itself is implemented in C ++ language, non-Sousse IO and the underlying mechanisms in the event loop Node are implemented by libuv.

 IOCP, it plainly IOCP is a message queue. We imagine a good start in advance if N threads, so that they hold live, all user requests are posted to a message queue. After allowing the N threads one by one to get the message from the message queue and processed. As a result, it is possible to avoid no user requests to open a new thread, the thread not only reduces the resources, but also improve the utilization of the thread.

libuv Chart

 

 

 

In Window environment, libuv directly using Windows IOCP to implement asynchronous IO. In the non-Windows environment, libuv use multiple threads to simulate asynchronous IO.

Note the following I have to say, an asynchronous call Node is supported by libuv to the above example to read the file, the file system calls essence is libuv done, but is responsible for calling libuv Node interface, and other data and then return to perform the corresponding callback method.

Node.js thread creation

Until Node 10.5.0 release, the official was given an experimental module worker_threads nature to Node provides true multi-threading capabilities.

Look at the simple demo:

  1. const {

  2. isMainThread,

  3. parentPort,

  4. workerData,

  5. threadId,

  6. MessageChannel,

  7. MessagePort,

  8. Worker

  9. } = require('worker_threads');

  10.  

  11. function mainThread() {

  12. for (let i = 0; i < 5; i++) {

  13. const worker = new Worker(__filename, { workerData: i });

  14. worker.on('exit', code => { console.log(`main: worker stopped with exit code ${code}`); });

  15. worker.on('message', msg => {

  16. console.log(`main: receive ${msg}`);

  17. worker.postMessage(msg + 1);

  18. });

  19. }

  20. }

  21.  

  22. function workerThread() {

  23. console.log(`worker: workerDate ${workerData}`);

  24. parentPort.on('message', msg => {

  25. console.log(`worker: receive ${msg}`);

  26. }),

  27. parentPort.postMessage(workerData);

  28. }

  29.  

  30. if (isMainThread) {

  31. mainThread();

  32. } else {

  33. workerThread();

  34. }

The code Open five child thread in the main thread and the main thread sends Cheng Xiangzai simple message.

Since worker_thread is still in the experimental stage, so it is necessary to increase --experimental-worker flag start, run Activity Monitor observed, opened five sub-thread

 

worker_thread module

workerthread core code (address https://github.com/nodejs/node/blob/master/lib/workerthreads.js)worker_thread module has four two objects and classes, can go above their source.

  • isMainThread: whether the main thread, the source code is determined by threadId === 0.

  • MessagePort: used for communication between threads, inherited from EventEmitter.

  • MessageChannel: for creating asynchronous, bidirectional communication channel instance.

  • threadId: thread ID.

  • Worker: used to create a child thread in the main thread. The first parameter is a filename, represents the entrance sub-thread execution.

  • parentPort: the worker thread is a type of the object MessagePort parent process, the main thread is null

  • workerData: for transferring data (data copy) to the child process in the main process

Guess you like

Origin www.cnblogs.com/seasonxin/p/11584456.html