TS spawn创建子进程
1.ts spawn创建进程
ts脚本 通过spawn创建一个子进程,并通过子管道进程通信。
child_process.spawn(command[, args][, options])#
History
command
args
options
cwd
env
. Default: false.
windowsHide
Returns:
The child_process.spawn() 使用“command”创建子进程,创建参数列表使用“args”,如果不传,使用空列表。如果启用了shell选项,请不要将未经初始化的用户输入传递到此函数。任何包含shell元字符的输入都可以用来触发任意命令的执行。
node.js 当前用覆盖argv[0]进程.execPath启动时,所以进程.argv[0]在nod.js子进程将与从父进程传递给spawn的argv0参数不匹配,请使process.argv0而不是属性。
options.stdio#
History
The options.stdio option 建立了用于配置进程之间的父管道和子管道的选项. 默认情况下, 子进程 stdin, stdout, and stderr 三个管道被重定向到 subprocess.stdin, subprocess.stdout, and subprocess.stderr streams on the ChildProcess 对象中. This is equivalent to setting the options.stdio equal to ['pipe', 'pipe', 'pipe'].
For convenience, options.stdio may be one of the following strings:
'pipe': equivalent to ['pipe', 'pipe', 'pipe'] (the default)
'ignore': equivalent to ['ignore', 'ignore', 'ignore']
'inherit': equivalent to ['inherit', 'inherit', 'inherit'] or [0, 1, 2]
否则,值选项.stdio是一个数组,其中每个索引对应于子级中的fd。fd 0、1和2分别对应于stdin、stdout和stderr。可以指定其他FD以在父对象和子对象之间创建其他管道。该值为以下值之一:
'pipe': Create a pipe between the child process and the parent process. The parent end of the pipe is exposed to the parent as a property on the child_process object as subprocess.stdio[fd]. Pipes created for fds 0, 1, and 2 are also available as subprocess.stdin, subprocess.stdout and subprocess.stderr, respectively.
'ipc': 创建用于在父级和子级之间传递消息/文件描述符的ipc通道。子进程最多可以有一个IPC stdio文件描述符。设置此选项将启用子流程.发送()方法。如果孩子是节点.js进程中,IPC通道的存在将启用进程.发送()和进程断开()方法,以及子级中的“断开连接”和“消息”事件。
Accessing the IPC channel fd in any way other than process.send() or using the IPC channel with a child process that is not a Node.js instance is not supported.
'ignore': Instructs Node.js to ignore the fd in the child. While Node.js will always open fds 0, 1, and 2 for the processes it spawns, setting the fd to 'ignore' will cause Node.js to open /dev/null and attach it to the child's fd.
'inherit': Pass through the corresponding stdio stream to/from the parent process. In the first three positions, this is equivalent to process.stdin, process.stdout, and process.stderr, respectively. In any other position, equivalent to 'ignore'.
Positive integer: The integer value is interpreted as a file descriptor that is currently open in the parent process. It is shared with the child process, similar to how
null, undefined: Use default value. For stdio fds 0, 1, and 2 (in other words, stdin, stdout, and stderr) a pipe is created. For fd 3 and up, the default is 'ignore'.
const { spawn } = require('child_process');
// Child will use parent's stdios.
spawn('prg', [], { stdio: 'inherit' });
// Spawn child sharing only stderr.
spawn('prg', [], { stdio: ['pipe', 'pipe', process.stderr] });
// Open an extra fd=4, to interact with programs presenting a
// startd-style interface.
spawn('prg', [], { stdio: ['pipe', null, null, null, 'pipe'] });
It is worth noting that when an IPC channel is established between the parent and child processes, and the child is a Node.js process, the child is launched with the IPC channel unreferenced (using unref()) until the child registers an event handler for the 'disconnect' event or the 'message' event. This allows the child to exit normally without the process being held open by the open IPC channel.
On Unix-like operating systems, the child_process.spawn() method performs memory operations synchronously before decoupling the event loop from the child. Applications with a large memory footprint may find frequent child_process.spawn() calls to be a bottleneck. For more information, see V8 issue 7381.
See also: child_process.exec() and child_process.fork().
2.mediasoup创建进程
mediasoup通过以下命令创建work子进程,并且通过它的管道和js master通信。tsmaster 创建7个管道用于和子进程通信,前三个分部是stdin,stdout,stderr,后两个是通道和负载。
如下是传递参数的个数:
https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
————————————————
版权声明:本文为CSDN博主「浴血筑梦」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Dreamandpassion/article/details/107777596