1. 程式人生 > 其它 >TS spawn建立子程序

TS spawn建立子程序

1.ts spawn建立程序
ts指令碼 通過spawn建立一個子程序,並通過子管道程序通訊。

child_process.spawn(command[, args][, options])#
History

command <string> 需要執行可執行的目錄
args <string[]> 引數列表
options <Object>

cwd <string> 子程序工作目錄.
env <Object> 子程序環境變數 env: { MEDIASOUP_VERSION: '3.6.14' },
argv0 <string> 強制設定程序的引數0引數,如果不設定,就取comand。
stdio <Array> | <string> 子程序標準輸出,很重要
detached <boolean> 子程序單獨執行,父程序殺死後,他任然存在
uid <number> 設定程序使用者標誌
gid <number> 設定程序使用者組標誌
serialization <string> 指定用於在程序之間傳送訊息的序列化型別。可能的值為“json”和“advanced”。有關詳細資訊,請參閱高階序列化. Default: 'json'。
shell <boolean> | <string> If true, 在shell中執行命令。在Unix上使用“/bin/sh”,並且過程.env.ComSpec在窗戶上。可以將另一個shell指定為字串。請參見Shell要求和預設Windows Shell。預設值:false(無shell)。
windowsVerbatimArguments <boolean>
在Windows上不引用或轉義引數。在Unix上被忽略。當指定shell和i時,這將自動設定為true

. Default: false.
windowsHide <boolean> 隱藏子程序視窗,預設為真. Default: false.
Returns: <ChildProcess>
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'.

<Stream> object: Share a readable or writable stream that refers to a tty, file, socket, or a pipe with the child process. The stream's underlying file descriptor is duplicated in the child process to the fd that corresponds to the index in the stdio array. The stream must have an underlying descriptor (file streams do not until the 'open' event has occurred).

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 <Stream> objects can be shared. Passing sockets is not supported on Windows.

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