Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/naptha/tesseract.js.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBalearica <admin@scribeocr.com>2022-08-10 08:34:18 +0300
committerGitHub <noreply@github.com>2022-08-10 08:34:18 +0300
commitbe956cd8898acedf792101811e0b3b00351fd8ea (patch)
tree0e10f1b6355f7bba8bc55ac3e173a57f5c84fb25
parent61d0e553c636c5eee0a2de51f74eb77092b06c64 (diff)
Replaced child_process with worker_threads per #630 (#631)
Replaced child_process with worker_threads per #630
-rw-r--r--examples/node/benchmark.js5
-rw-r--r--src/worker-script/node/index.js5
-rw-r--r--src/worker/node/send.js4
-rw-r--r--src/worker/node/spawnWorker.js9
-rw-r--r--src/worker/node/terminateWorker.js2
5 files changed, 9 insertions, 16 deletions
diff --git a/examples/node/benchmark.js b/examples/node/benchmark.js
index bbb2b74..728d1d5 100644
--- a/examples/node/benchmark.js
+++ b/examples/node/benchmark.js
@@ -2,9 +2,7 @@
const path = require('path');
const { createWorker } = require('../../');
-const worker = createWorker({
- // logger: m => console.log(m)
-});
+const worker = createWorker();
(async () => {
await worker.load();
@@ -23,7 +21,6 @@ const worker = createWorker({
console.log(file + " [x10] runtime: " + timeDif + "s");
}
-
console.log("Total runtime: " + timeTotal + "s");
await worker.terminate();
diff --git a/src/worker-script/node/index.js b/src/worker-script/node/index.js
index 15cb3fb..c00dba7 100644
--- a/src/worker-script/node/index.js
+++ b/src/worker-script/node/index.js
@@ -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');
/*
* 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({
diff --git a/src/worker/node/send.js b/src/worker/node/send.js
index 783c6e1..88f8aaf 100644
--- a/src/worker/node/send.js
+++ b/src/worker/node/send.js
@@ -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);
};
diff --git a/src/worker/node/spawnWorker.js b/src/worker/node/spawnWorker.js
index c538244..9723c18 100644
--- a/src/worker/node/spawnWorker.js
+++ b/src/worker/node/spawnWorker.js
@@ -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;
* @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);
diff --git a/src/worker/node/terminateWorker.js b/src/worker/node/terminateWorker.js
index 0e8b67e..834760e 100644
--- a/src/worker/node/terminateWorker.js
+++ b/src/worker/node/terminateWorker.js
@@ -6,5 +6,5 @@
* @access public
*/
module.exports = (worker) => {
- worker.kill();
+ worker.terminate();
};