Page 116 - Nodejs 교과서 개정2판
P. 116
§ Ӓܿ ݫੋ झۨ٘৬ ਕழ ాन
worker_data.js
const {
Worker, isMainThread, parentPort, workerData,
} = require('worker_threads');
if (isMainThread) { // 부모일 때
const threads = new Set();
threads.add(new Worker(__filename, {
workerData: { start: 1 },
}));
threads.add(new Worker(__filename, {
workerData: { start: 2 },
}));
for (let worker of threads) {
worker.on('message', message => console.log('from worker', message));
worker.on('exit', () => {
threads.delete(worker);
if (threads.size === 0) {
console.log('job done');
}
});
}
} else { // 워커일 때
const data = workerData;
parentPort.postMessage(data.start + 100);
}
new Workerܳ ഐೡ ٸ ف ߣ૩ ੋࣻ workerData ࣘࢿਵ۽ ਗೞח ؘఠܳ ࠁյ ࣻ णפ ਕழীࢲח w
orkerData۽ ࠗݽ۽ࠗఠ ؘఠܳ ߉णפ അ ف ѐ ਕழо جইоҊ ਵݴ пп ࠗݽ۽ࠗఠ ंܳ ߉
ইࢲ 100ਸ ؊೧ ج۰સפ ج۰ח ࣽр ਕழо ઙܐغয worker.on('exit') प೯ؾפ ਕழ ف ѐо
ݽف ઙܐغݶ job done ۽Ӧؾפ
콘솔