Parent/Child 从 js 文件处理或 运行 npm 任务
Parent/Child process or run npm task from js file
我对 child-process
有点陌生,需要进一步说明。
我的案例
我正在寻找将一个 js
文件 run/trigger 作为另一个 js
文件的 separate/isolated 进程的方法。不使用直接 require
或 import as
,而是将某个参数(在我的例子中为数字)从 parent 传递到 child 进程。
parent.js
async function parent () {
/** logic */
/** run/start child.js script with certain (1) argument from logic above /
/** if logic below fails, it won't crash/trigger child, and his job will be finished separately outside async function*/
}
child.js
async function child(number) {
/** receive argument from parent.js and execute separately*/
}
只是为了确定,架构:
MAIN THREAD
|
|
run child => CHILD THREAD (receive number arg from main thread)
| |
x | (don't care about error in main thread)
(error) |
END OF M_THREAD |
|
V
(ok, filled)
所以我已经阅读了很多关于 child_process
和官方 node.js
docs 的文章,并且知道有:
- 分叉
- 执行
- child
模式。我也可以 run a separate npm task 从我的 parent.js
文件(例如通过 pm2),但我不知道在这种情况下我应该使用什么以及什么适合我的需要。所以任何建议,解释都会有所帮助,并且会得到我up-voting的奖励。
您可以在此处查看,提供了如何执行此操作的简单示例。 https://zaiste.net/nodejs-child-process-spawn-exec-fork-async-await/
希望对你有帮助。
我对 child-process
有点陌生,需要进一步说明。
我的案例
我正在寻找将一个 js
文件 run/trigger 作为另一个 js
文件的 separate/isolated 进程的方法。不使用直接 require
或 import as
,而是将某个参数(在我的例子中为数字)从 parent 传递到 child 进程。
parent.js
async function parent () {
/** logic */
/** run/start child.js script with certain (1) argument from logic above /
/** if logic below fails, it won't crash/trigger child, and his job will be finished separately outside async function*/
}
child.js
async function child(number) {
/** receive argument from parent.js and execute separately*/
}
只是为了确定,架构:
MAIN THREAD
|
|
run child => CHILD THREAD (receive number arg from main thread)
| |
x | (don't care about error in main thread)
(error) |
END OF M_THREAD |
|
V
(ok, filled)
所以我已经阅读了很多关于 child_process
和官方 node.js
docs 的文章,并且知道有:
- 分叉
- 执行
- child
模式。我也可以 run a separate npm task 从我的 parent.js
文件(例如通过 pm2),但我不知道在这种情况下我应该使用什么以及什么适合我的需要。所以任何建议,解释都会有所帮助,并且会得到我up-voting的奖励。
您可以在此处查看,提供了如何执行此操作的简单示例。 https://zaiste.net/nodejs-child-process-spawn-exec-fork-async-await/ 希望对你有帮助。