An article exploring processes and sub-processes in Node

青灯夜游
Release: 2022-10-12 19:32:09
forward
1840 people have browsed it

This article will give you an in-depth understanding of the processes and sub-processes ofNode. I hope it will be helpful to everyone!

An article exploring processes and sub-processes in Node

Process: process module

The process module is a tool provided by nodejs for developers to interact with the current process. Provides many practical APIs. Start from the documentation, get a glimpse of the leopard, and further understand and learn the process module:

  • How to process command parameters?
  • How to deal with the working directory?
  • How to handle exceptions?
  • How to handle process exit?
  • The standard stream object of process
  • In-depth understanding of process.nextTick

[Related tutorial recommendations:nodejs video tutorial]

How to handle command parameters?

Command line parameters refer to 2 aspects:

  • Parameters passed to node. For example,node --harmony script.js --version,--harmonyis the parameter passed to node
  • The parameter passed to the process. For example, innode script.js --version --help,--version --helpis the parameter passed to the process

They pass# respectively ##process.argvandprocess.execArgvto obtain.

How to deal with the working directory?

The current working directory can be obtained through

process.cwd().

You can switch the current working directory through

process.chdir(directory), and an exception will be thrown if it fails. The practice is as follows:

function safeChdir(dir) { try { process.chdir(dir); return true; } catch (error) { return false; } }
Copy after login

How to handle exceptions?

uncaughtException event

Nodejs can catch exceptions through try-catch. If the exception is not caught, it will always bubble up from the bottom to the event loop. If the exception that bubbles up to the event loop is not handled, it will cause the current process to exit abnormally.

According to the documentation, you can handle uncaught exceptions by listening to the uncaughtException event of the process:

process.on("uncaughtException", (err, origin) => { console.log(err.message); }); const a = 1 / b; console.log("abc"); // 不会执行
Copy after login
In the above code, the output of the console is:

b is not defined. The error message was caught and the process exited with0. Developers can clear some allocated resources (file descriptors, handles, etc.) in the uncaughtException event, and restarting the process is not recommended.

unhandledRejection event

If a Promise callback exception is not caught by

.catch(), then the unhandledRejection event of the process will be triggered:

process.on("unhandledRejection", (err, promise) => { console.log(err.message); }); Promise.reject(new Error("错误信息")); // 未被catch捕获的异常,交由unhandledRejection事件处理
Copy after login

warning Event

Warnings are not a formal part of the Node.js and Javascript error handling process. Node.js can issue alerts once it detects coding practices that may lead to application performance issues, defects, or security risks.

For example, in the previous code, if an uncaught promise callback exception occurs, the warning event will be triggered.

How to handle process exit?

process.exit() vs process.exitCode

A nodejs process can specify the exit code through process.exit() and exit directly.

It is not recommended to use process.exit()directly, which will cause the tasks in the event loop to not be processed directly, and may lead to truncation and loss of data (such as the writing of stdout).

setTimeout(() => { console.log("我不会执行"); }); process.exit(0);
Copy after login

The correct and safe handling is to, set process.exitCode, and allow the process to exit naturally.

setTimeout(() => { console.log("我不会执行"); }); process.exitCode = 1;
Copy after login

beforeExit event

The events used to handle process exit are: beforeExit event and exit event.

The beforeExit event is fired when Node.js clears its event loop and has no more work to schedule. For example, if you need some asynchronous operations before exiting, you can write it in the beforeExit event:

let hasSend = false; process.on("beforeExit", () => { if (hasSend) return; // 避免死循环 setTimeout(() => { console.log("mock send data to serve"); hasSend = true; }, 500); }); console.log("......."); // 输出: // ....... // mock send data to serve
Copy after login
Note: If it is an asynchronous task in the beforeExit event, it will be added to the task queue. At this time, after the task queue completes all tasks, the beforeExit event is triggered again. Therefore, if it is not handled,

an infinite loop may occur. If exit() is called explicitly, this event will not be triggered.

exit event

In the exit event, only synchronous operations can be performed. After calling the 'exit' event listener, the Node.js process will exit immediately, causing any other work still queued in the event loop to be abandoned.

process’s standard stream objects

process provides 3 standard streams. Note that some of them are synchronously blocking at some point (see documentation).

  • process.stderr:WriteStream 类型,console.error的底层实现,默认对应屏幕
  • process.stdout:WriteStream 类型,console.log的底层实现,默认对应屏幕
  • process.stdin:ReadStream 类型,默认对应键盘输入

下面是基于“生产者-消费者模型”的读取控制台输入并且及时输出的代码:

process.stdin.setEncoding("utf8"); process.stdin.on("readable", () => { let chunk; while ((chunk = process.stdin.read()) !== null) { process.stdout.write(`>>> ${chunk}`); } }); process.stdin.on("end", () => { process.stdout.write("结束"); });
Copy after login

关于事件的含义,还是请看stream 的文档。

深入理解 process.nextTick

我第一次看到 process.nextTick 的时候是比较懵的,看文档可以知道,它的用途是:把回调函数作为微任务,放入事件循环的任务队列中。但这么做的意义是什么呢?

因为 nodejs 并不适合计算密集型的应用,一个进程就一个线程,在当下时间点上,就一个事件在执行。那么,如果我们的事件占用了很多 cpu 时间,那么之后的事件就要等待非常久。所以,nodejs 的一个编程原则是尽量缩短每一个事件的执行事件。process.nextTick 的作用就在这,将一个大的任务分解成多个小的任务。示例代码如下:

// 被拆分成2个函数执行 function BigThing() { doPartThing(); process.nextTick(() => finishThing()); }
Copy after login

在事件循环中,何时执行 nextTick 注册的任务呢?请看下面的代码:

setTimeout(function() { console.log("第一个1秒"); process.nextTick(function() { console.log("第一个1秒:nextTick"); }); }, 1000); setTimeout(function() { console.log("第2个1秒"); }, 1000); console.log("我要输出1"); process.nextTick(function() { console.log("nextTick"); }); console.log("我要输出2");
Copy after login

输出的结果如下,nextTick 是早于 setTimeout:

我要输出1 我要输出2 nextTick 第一个1秒 第一个1秒:nextTick 第2个1秒
Copy after login

在浏览器端,nextTick 会退化成setTimeout(callback, 0)。但在 nodejs 中请使用 nextTick 而不是 setTimeout,前者效率更高,并且严格来说,两者创建的事件在任务队列中顺序并不一样(请看前面的代码)。

子进程:child_process模块

掌握 nodejs 的 child_process 模块能够极大提高 nodejs 的开发能力,例如主从进程来优化 CPU 计算的问题,多进程开发等等。本文从以下几个方面介绍 child_process 模块的使用:

  • 创建子进程
  • 父子进程通信
  • 独立子进程
  • 进程管道

创建子进程

nodejs 的 child_process 模块创建子进程的方法:spawn, fork, exec, execFile。它们的关系如下:

  • fork, exec, execFile 都是通过 spawn 来实现的。
  • exec 默认会创建 shell。execFile 默认不会创建 shell,意味着不能使用 I/O 重定向、file glob,但效率更高。
  • spawn、exec、execFile 都有同步版本,可能会造成进程阻塞。

child_process.spawn()的使用:

const { spawn } = require("child_process"); // 返回ChildProcess对象,默认情况下其上的stdio不为null const ls = spawn("ls", ["-lh"]); ls.stdout.on("data", data => { console.log(`stdout: ${data}`); }); ls.stderr.on("data", data => { console.error(`stderr: ${data}`); }); ls.on("close", code => { console.log(`子进程退出,退出码 ${code}`); });
Copy after login

child_process.exec()的使用:

const { exec } = require("child_process"); // 通过回调函数来操作stdio exec("ls -lh", (err, stdout, stderr) => { if (err) { console.error(`执行的错误: ${err}`); return; } console.log(`stdout: ${stdout}`); console.error(`stderr: ${stderr}`); });
Copy after login

父子进程通信

fork()返回的 ChildProcess 对象,监听其上的 message 事件,来接受子进程消息;调用 send 方法,来实现 IPC。

parent.js 代码如下:

const { fork } = require("child_process"); const cp = fork("./sub.js"); cp.on("message", msg => { console.log("父进程收到消息:", msg); }); cp.send("我是父进程");
Copy after login

sub.js 代码如下:

process.on("message", m => { console.log("子进程收到消息:", m); }); process.send("我是子进程");
Copy after login

运行后结果:

父进程收到消息: 我是子进程 子进程收到消息: 我是父进程
Copy after login

独立子进程

在正常情况下,父进程一定会等待子进程退出后,才退出。如果想让父进程先退出,不受到子进程的影响,那么应该:

  • 调用 ChildProcess 对象上的unref()
  • options.detached设置为 true
  • 子进程的 stdio 不能是连接到父进程

main.js 代码如下:

const { spawn } = require("child_process"); const subprocess = spawn(process.argv0, ["sub.js"], { detached: true, stdio: "ignore" }); subprocess.unref();
Copy after login

sub.js 代码如下:

setInterval(() => {}, 1000);
Copy after login

进程管道

options.stdio 选项用于配置在父进程和子进程之间建立的管道。 默认情况下,子进程的 stdin、 stdout 和 stderr 会被重定向到 ChildProcess 对象上相应的 subprocess.stdin、subprocess.stdout 和 subprocess.stderr 流。 这意味着可以通过监听其上的data事件,在父进程中获取子进程的 I/O 。

可以用来实现“重定向”:

const fs = require("fs"); const child_process = require("child_process"); const subprocess = child_process.spawn("ls", { stdio: [ 0, // 使用父进程的 stdin 用于子进程。 "pipe", // 把子进程的 stdout 通过管道传到父进程 。 fs.openSync("err.out", "w") // 把子进程的 stderr 定向到一个文件。 ] });
Copy after login

也可以用来实现"管道运算符":

const { spawn } = require("child_process"); const ps = spawn("ps", ["ax"]); const grep = spawn("grep", ["ssh"]); ps.stdout.on("data", data => { grep.stdin.write(data); }); ps.stderr.on("data", err => { console.error(`ps stderr: ${err}`); }); ps.on("close", code => { if (code !== 0) { console.log(`ps 进程退出,退出码 ${code}`); } grep.stdin.end(); }); grep.stdout.on("data", data => { console.log(data.toString()); }); grep.stderr.on("data", data => { console.error(`grep stderr: ${data}`); }); grep.on("close", code => { if (code !== 0) { console.log(`grep 进程退出,退出码 ${code}`); } });
Copy after login

更多node相关知识,请访问:nodejs 教程

The above is the detailed content of An article exploring processes and sub-processes in Node. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!