Compare commits

...

3 Commits

Author SHA1 Message Date
Your Name 0a4b4d77af Minor edit 2 years ago
Your Name 2a874bfc37 Removed comment 2 years ago
Your Name 37ed99a130 Replaced child_process with worker_threads per #630 2 years ago
  1. 5
      examples/node/benchmark.js
  2. 5
      src/worker-script/node/index.js
  3. 4
      src/worker/node/send.js
  4. 9
      src/worker/node/spawnWorker.js
  5. 2
      src/worker/node/terminateWorker.js

5
examples/node/benchmark.js

@ -2,9 +2,7 @@
const path = require('path'); const path = require('path');
const { createWorker } = require('../../'); const { createWorker } = require('../../');
const worker = createWorker({ const worker = createWorker();
// logger: m => console.log(m)
});
(async () => { (async () => {
await worker.load(); await worker.load();
@ -23,7 +21,6 @@ const worker = createWorker({
console.log(file + " [x10] runtime: " + timeDif + "s"); console.log(file + " [x10] runtime: " + timeDif + "s");
} }
console.log("Total runtime: " + timeTotal + "s"); console.log("Total runtime: " + timeTotal + "s");
await worker.terminate(); await worker.terminate();

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

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

4
src/worker/node/send.js

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

9
src/worker/node/spawnWorker.js

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

2
src/worker/node/terminateWorker.js

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

Loading…
Cancel
Save