Browse Source

Replaced child_process with worker_threads per #630

feat/worker_threads
Your Name 2 years ago
parent
commit
37ed99a130
  1. 5
      src/worker-script/node/index.js
  2. 4
      src/worker/node/send.js
  3. 9
      src/worker/node/spawnWorker.js
  4. 2
      src/worker/node/terminateWorker.js

5
src/worker-script/node/index.js

@ -9,6 +9,7 @@ @@ -9,6 +9,7 @@
*/
const fetch = require('node-fetch');
const { parentPort } = require('worker_threads');
const worker = require('..');
const getCore = require('./getCore');
const gunzip = require('./gunzip');
@ -17,8 +18,8 @@ const cache = require('./cache'); @@ -17,8 +18,8 @@ const cache = require('./cache');
/*
* register message handler
*/
process.on('message', (packet) => {
worker.dispatchHandlers(packet, (obj) => process.send(obj));
parentPort.on('message', (packet) => {
worker.dispatchHandlers(packet, (obj) => parentPort.postMessage(obj));
});
worker.setAdapter({

4
src/worker/node/send.js

@ -5,6 +5,6 @@ @@ -5,6 +5,6 @@
* @function send packet to worker and create a job
* @access public
*/
module.exports = (worker, packet) => {
worker.send(packet);
module.exports = async (worker, packet) => {
worker.postMessage(packet);
};

9
src/worker/node/spawnWorker.js

@ -1,6 +1,4 @@ @@ -1,6 +1,4 @@
const { fork } = require('child_process');
let debugPort = 9229;
const { Worker } = require('worker_threads');
/**
* spawnWorker
@ -9,7 +7,4 @@ let debugPort = 9229; @@ -9,7 +7,4 @@ let debugPort = 9229;
* @function fork a new process in node
* @access public
*/
module.exports = ({ workerPath }) => {
debugPort += 1;
return fork(workerPath, [], { execArgv: [`--debug-port=${debugPort}`] });
};
module.exports = ({ workerPath }) => new Worker(workerPath);

2
src/worker/node/terminateWorker.js

@ -6,5 +6,5 @@ @@ -6,5 +6,5 @@
* @access public
*/
module.exports = (worker) => {
worker.kill();
worker.terminate();
};

Loading…
Cancel
Save