From 9460758cf523cc461a434df4e253eb70f53d8136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=98=A0=E5=B9=B3?= Date: Thu, 24 Mar 2022 21:25:58 +0800 Subject: [PATCH] feat: getting started example of worker_threads --- .../getting-started/index.js | 16 +++++++++ .../getting-started/json-util.js | 35 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 examples/2022.03.24-worker-threads/getting-started/index.js create mode 100644 examples/2022.03.24-worker-threads/getting-started/json-util.js diff --git a/examples/2022.03.24-worker-threads/getting-started/index.js b/examples/2022.03.24-worker-threads/getting-started/index.js new file mode 100644 index 0000000..97f6ed8 --- /dev/null +++ b/examples/2022.03.24-worker-threads/getting-started/index.js @@ -0,0 +1,16 @@ +const stringify = require('./json-util'); + +stringify({"uid": "10001"}); +stringify({"uid": "10002"}); + +/* + 输出日志如下,当 worker 通过 postMessage 给主线程发消息,1、父线程受到消息,并进入父线程的回调;2、父线程回调结束,子线程继续执行(postMessage后的逻辑) + [main thread] before processing, process.pid = 61000, thread id = 0, uid = 10001 + [main thread] before processing, process.pid = 61000, thread id = 0, uid = 10002 + [worker thread] start processing, process id = 61000, thread id = 1, uid = 10001 + [main thread] message from worker[threadId = 1], process.pid = 61000, thread id = 0, uid = 10001 + [worker thread] start processing, process id = 61000, thread id = 2, uid = 10002 + [main thread] message from worker[threadId = 2], process.pid = 61000, thread id = 0, uid = 10002 + [worker thread] finishing processing, process id = 61000, thread id = 1, uid = 10001 + [worker thread] finishing processing, process id = 61000, thread id = 2, uid = 10002 +*/ \ No newline at end of file diff --git a/examples/2022.03.24-worker-threads/getting-started/json-util.js b/examples/2022.03.24-worker-threads/getting-started/json-util.js new file mode 100644 index 0000000..a06e99d --- /dev/null +++ b/examples/2022.03.24-worker-threads/getting-started/json-util.js @@ -0,0 +1,35 @@ +const { + Worker, isMainThread, parentPort, workerData, threadId + } = require('worker_threads'); + + if (isMainThread) { + module.exports = function stringify(script) { + console.log(`[main thread] before processing, process.pid = ${process.pid}, thread id = ${threadId}, uid = ${script.uid}`); + return new Promise((resolve, reject) => { + const worker = new Worker(__filename, { + workerData: script + }); + worker.on('message', (result) => { + console.log(`[main thread] message from worker[threadId = ${result.threadId}], process.pid = ${process.pid}, thread id = ${threadId}, uid = ${script.uid}`); + resolve(result) + }); + worker.on('error', reject); + worker.on('exit', (code) => { + if (code !== 0) + reject(new Error(`Worker stopped with exit code ${code}`)); + }); + }); + }; + } else { + const threadId = require('worker_threads').threadId; + console.log(`\t[worker thread] start processing, process id = ${process.pid}, thread id = ${threadId}, uid = ${workerData.uid}`); + + function parse(jsonObj) { + return JSON.stringify(jsonObj); + } + + const script = workerData; + parentPort.postMessage({ jsonStr: parse(script), threadId: threadId } ); + + console.log(`\t[worker thread] finishing processing, process id = ${process.pid}, thread id = ${threadId}, uid = ${workerData.uid}`); + } \ No newline at end of file