什么是节点子进程? [英] What is a node child process?

查看:85
本文介绍了什么是节点子进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一个node.js项目,其中包括 child_process 库。

I'm reading a node.js project which includes the child_process library.

什么是子进程?

在子流程中运行流程而不是简单地正常执行流程有什么优势?我假设这是如何为您提供更多访问内存的权限?

What advantages are there to running processes in a child process rather than simply executing it normally? I'm assuming this some how gives you more access to memory?

推荐答案

在Linux上运行终端(bash进程)时,并执行一个命令,例如 ls -lh / usr ,终端启动一个子进程 ls ,该子进程将写入 stdout 当前目录中的所有文件。现在,将其镜像而不是终端,您将 node.js 作为父进程。您可以像这样生成/启动子 ls 进程:

When you run terminal on Linux (bash process), and execute a command, for example ls -lh /usr, the terminal starts a child process ls, that writes to stdout all files in the current directory. Now image that instead of a terminal, you have node.js as a parent process. You can spawn/start a child ls process like this:

const spawn = require('child_process').spawn;
const ls = spawn('ls', ['-lh', '/usr']);

ls.stdout.on('data', (data) => {
  console.log(`stdout: ${data}`);
});

ls.stderr.on('data', (data) => {
  console.log(`stderr: ${data}`);
});

ls.on('close', (code) => {
  console.log(`child process exited with code ${code}`);
});




这类似于JavaScript网络工作者吗?

Is this similar a javascript web worker?

它可能与网络工作者相似,但是我不知道如何在浏览器中实现网络工作者。 AFAIK 节点没有开箱即用的Webworkers API。但是,如果您的子进程是 node.js 进程,则可以查看此子进程,类似于 webworker 。看看这个集群API

It might be similar to webworkers, but I don't know how webworkers are implemented under the hood in browsers. AFAIK node doesn't have webworkers API out of the box. But if your child process is node.js process, than you can view this child process something akin to webworker. Take a look at this cluster API.

这篇关于什么是节点子进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆