worker_thread
worker_thread最佳实践
//基于worker_threads开启多线程工作
this._worker = new Worker(path.resolve(__dirname, './threadChild.js'), {
eval: false,
stderr: true,
stdout: true,
workerData: {
cwd: process.cwd(),
env: {
...process.env,
JEST_WORKER_ID: String(this._options.workerId + 1), // 0-indexed workerId, 1-indexed JEST_WORKER_ID
} as NodeJS.ProcessEnv,
// Suppress --debug / --inspect flags while preserving others (like --harmony).
execArgv: process.execArgv.filter(v => !/^--(debug|inspect)/.test(v)),
silent: true,
...this._options.forkOptions,
},
});
if (this._worker.stdout) {
if (!this._stdout) {
// We need to add a permanent stream to the merged stream to prevent it
// from ending when the subprocess stream ends
this._stdout = mergeStream(this._getFakeStream());
}
this._stdout.add(this._worker.stdout);
}
if (this._worker.stderr) {
if (!this._stderr) {
// We need to add a permanent stream to the merged stream to prevent it
// from ending when the subprocess stream ends
this._stderr = mergeStream(this._getFakeStream());
}
this._stderr.add(this._worker.stderr);
}
this._worker.on('message', this.onMessage.bind(this));
this._worker.on('exit', this.onExit.bind(this));
this._worker.postMessage([
CHILD_MESSAGE_INITIALIZE,
false,
this._options.workerPath,
this._options.setupArgs,
]);
其子线程threadChild.js:
import {parentPort, isMainThread} from 'worker_threads';
parentPort!.on('message', (request: any) => {
switch (request[0]) {
case CHILD_MESSAGE_INITIALIZE:
const init: ChildMessageInitialize = request;
file = init[2];
setupArgs = request[3];
break;
case CHILD_MESSAGE_CALL:
const call: ChildMessageCall = request;
execMethod(call[2], call[3]);
break;
case CHILD_MESSAGE_END:
end();
break;
default:
throw new TypeError(
'Unexpected request from parent process: ' + request[0],
);
}
});