How to use the fork function from child_process
Find comprehensive JavaScript child_process.fork code examples handpicked from public code repositorys.
child_process.fork is a Node.js module method that creates a new Node.js process and runs a specified module in that process.
GitHub: txd-team/docsite
67 68 69 70 71 72 73 74 75 76
const start = () => { shell.cd(CWD); generateJSONFile('dev', CWD); generateHTMLFile('dev', CWD); cp.fork(path.join(__dirname, './watch.js')); const platform = process.platform; if (platform === 'win32') { shell.exec(`node ${['node_modules', 'gulp', 'bin', 'gulp.js'].join(path.sep)}`); } else {
325 326 327 328 329 330 331 332 333
console.log('Adapter installed.'); if (cb) cb(); }); } else { // add controller const _pid = child_process.fork(startFile, ['add', customName, '--enabled', 'false'], { cwd: rootDir + 'tmp', stdio: [0, 1, 2, 'ipc'] });
+ 11 other calls in file
How does child_process.fork work?
child_process.fork
is a method in Node.js that creates a new process that runs a specified module and provides a communication channel between the parent and child processes, allowing for inter-process communication through messaging.
GitHub: nyx-bot/Nyx
175 176 177 178 179 180 181 182 183 184
const runProc = () => new Promise(async res => { lastStarted = Date.now(); let args = [`nyxfork`] nyx = fork(`./bot.js`, args, { detached: false, silent: true, serialization: `advanced` });
GitHub: NodeBB/NodeBB
139 140 141 142 143 144 145 146 147 148
if (inspect) { execArgv.push(inspect); } worker = fork('app.js', args, { env, execArgv, }); }
Ai Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
const { fork } = require("child_process"); // Spawn a new process running 'child.js' const child = fork("child.js"); // Send a message to the child process child.send("Hello from the parent process!"); // Listen for messages from the child process child.on("message", (message) => { console.log(`Received message from child process: ${message}`); }); // Listen for the child process to exit child.on("exit", (code) => { console.log(`Child process exited with code ${code}`); });
In this example, child.js is a separate Node.js file that will be executed as a child process. The fork method returns a ChildProcess instance, which can be used to communicate with the child process using send and on('message'). Finally, the parent process listens for the child process to exit using on('exit').
77 78 79 80 81 82 83 84 85 86 87
}); // pendingKillers.forEach((fn) => fn()); // pendingKillers.length = 0; // const modulePath = config.output.path + "\\" + targetApp + ".js", // res = fork(modulePath, { // killSignal: "SIGTERM", // }); // pendingKillers.push(() => res.kill("SIGTERM")); }
+ 4 other calls in file
47 48 49 50 51 52 53 54 55 56
} if(req.body.email == undefined){ res.json({message: "no email"}); return; } const forked_child_process = childProcess.fork('./catalogue.js'); // send message to the child process forked_child_process.send({EANS:req.body.EANS, name: req.body.name, email: req.body.email, headless: true}); // listen for a response from the child process forked_child_process.on("message", id => res.json({data: id, message: "everything is ok"}));
+ 9 other calls in file
GitHub: Crawlerop/dtvbackend-2
61 62 63 64 65 66 67 68 69 70
} if (!found) return sid.reject() const out_path = `${path.resolve(config.streams_path.replace(/\(pathname\)/g, __dirname+"/../"))}/${found_id}/` await fs.mkdir(out_path, {recursive: true}) const cur_proc = cp.fork(path.resolve(path.join(__dirname, "/../scripts/rtmp.js"))) cur_proc.on("message", (d) => { if (!d.retry) { try {
GitHub: Crawlerop/dtvbackend-2
28 29 30 31 32 33 34 35 36 37
return {error: "Stream is already recording"} } } else { if (DVR_STREAMS[stream_id] === undefined) { DVR_STREAMS[stream_id] = crypto.randomBytes(64).toString("hex") DVR_PROC[DVR_STREAMS[stream_id]] = cp.fork(path.resolve(path.join(__dirname, "/../scripts/dvr.js"))) DVR_PROC[DVR_STREAMS[stream_id]].send({ stream_id: stream_id, channel: -1, target: DVR_STREAMS[stream_id]
307 308 309 310 311 312 313 314 315 316
try { // console.log("request for getting base data"); let user = req.user; let issended=false; let child = fork('./DB/dbwebops.js'); child.on("message", (result) => { issended=true; res.json(result);
+ 5 other calls in file
child_process.execSync is the most popular function in child_process (240 examples)