父/子进程或从js文件运行npm任务

问题描述 投票:0回答:1

我对child-process有点陌生,需要对此进行其他说明。

我的案子

我正在寻找一种方法来将一个js文件与另一个js文件作为单独/隔离的进程运行/触发。不使用直接的requireimport as,而是从父进程到子进程传递某个参数(在我的情况下为数字)。

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的文章,并且知道有:

  • fork
  • exec

模式。我也可以从un a separate npm task文件中获取r parent.js(例如通过pm2),但是我不知道在这种情况下应该使用什么以及适合我的需求。因此,任何建议,解释将是有帮助的,并会通过我的投票获得回报。

javascript node.js child-process
1个回答
1
投票

您可以在此处进行检查,并提供简单的示例。https://zaiste.net/nodejs-child-process-spawn-exec-fork-async-await/希望对您有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.