Child Process Function

时间:2017-08-14 07:09:44

标签: javascript node.js child-process

I have main APP and child JS. I want to run child JS in child Process this child.js is an schedule loop to check every hour on db.

How to do it?

I will post the Quick Code

Child.js

exports.main = () => {
  console.log('Running API DAEMON')
  updateInventoryInit();
}
function updateInventoryInit(){ //something happen };

Main_APP.js

var api_DAEMON = require('./api_daemon');
api_DAEMON.main();

1 个答案:

答案 0 :(得分:0)

据我所知,您可以尝试以下代码:

<强> main.js

 const cp = require('child_process');

 let child = cp.fork(__dirname + "/child.js");

 child.on('message', (msg) => {
     console.log("from child : ", msg);
 }); 

 setInterval( () => { 
      // sent msg to child in every hour  
      child.send( 'check-db' );

 }, 'every-hour');

<强> child.js

// receive msg
process.on("message", (data) => {

   // perform your task

   // after complete task
   process.send("complete");

});