node-worker-threads-pool
Advanced tools
Comparing version 1.2.2 to 1.3.0
@@ -14,2 +14,5 @@ /** | ||
workerData?: any; | ||
/** enable SHARE_ENV for all threads in pool */ | ||
shareEnv?: boolean; | ||
}); | ||
@@ -36,4 +39,11 @@ | ||
* @param size number of threads. | ||
* @param opt additional options. | ||
*/ | ||
constructor(size: number); | ||
constructor( | ||
size: number, | ||
opt?: { | ||
/** enable SHARE_ENV for all threads in pool */ | ||
shareEnv?: boolean; | ||
} | ||
); | ||
@@ -40,0 +50,0 @@ /** |
{ | ||
"name": "node-worker-threads-pool", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "Simple worker threads pool using Node's worker_threads module. Compatible with ES6+ Promise, Async/Await.", | ||
@@ -13,3 +13,3 @@ "main": "index.js", | ||
"dynamic": "node --experimental-worker ./example/dynamic", | ||
"test": "jest" | ||
"test": "jest --coverage" | ||
}, | ||
@@ -39,4 +39,4 @@ "repository": { | ||
"devDependencies": { | ||
"husky": "^4.2.3", | ||
"jest": "^24.8.0", | ||
"husky": "^4.2.5", | ||
"jest": "^24.9.0", | ||
"prettier": "1.19.1", | ||
@@ -43,0 +43,0 @@ "pretty-quick": "^2.0.1" |
@@ -24,5 +24,13 @@ const Pool = require("./pool"); | ||
module.exports = class DynamicPool extends Pool { | ||
constructor(size) { | ||
constructor(size, opt) { | ||
super(size); | ||
this.fill(() => new PoolWorker(script, { eval: true })); | ||
const workerOpt = { eval: true }; | ||
if (opt) { | ||
/* istanbul ignore next */ | ||
if (opt.shareEnv) { | ||
const { SHARE_ENV } = require("worker_threads"); | ||
workerOpt.env = SHARE_ENV; | ||
} | ||
} | ||
this.fill(() => new PoolWorker(script, workerOpt)); | ||
} | ||
@@ -29,0 +37,0 @@ |
@@ -109,6 +109,6 @@ const Events = require("events"); | ||
if (worker) { | ||
const p = new PromiseWithTimer(worker.work(task), timeout); | ||
let res; | ||
try { | ||
res = await p.start(); | ||
const p = new PromiseWithTimer(worker.work(task), timeout); | ||
const res = await p.start(); | ||
return res; | ||
} catch (err) { | ||
@@ -120,3 +120,2 @@ if (isTimeoutError(err)) { | ||
} | ||
return res; | ||
} | ||
@@ -123,0 +122,0 @@ |
@@ -46,9 +46,21 @@ const Pool = require("./pool"); | ||
* @param { * } opt.workerData data to pass into workers | ||
* @param { Boolean } opt.shareEnv enable SHARE_ENV for all threads in pool | ||
*/ | ||
constructor({ size, task, workerData }) { | ||
constructor({ size, task, workerData, shareEnv }) { | ||
super(size); | ||
const workerOpt = { workerData }; | ||
/* istanbul ignore next */ | ||
if (shareEnv) { | ||
const { SHARE_ENV } = require("worker_threads"); | ||
workerOpt.env = SHARE_ENV; | ||
} | ||
if (typeof task === "function") { | ||
workerOpt.eval = true; | ||
} | ||
switch (typeof task) { | ||
case "string": { | ||
// task is the path of worker script. | ||
this.fill(() => new PoolWorker(task, { workerData })); | ||
this.fill(() => new PoolWorker(task, workerOpt)); | ||
break; | ||
@@ -59,3 +71,3 @@ } | ||
const script = createScript(task); | ||
this.fill(() => new PoolWorker(script, { eval: true, workerData })); | ||
this.fill(() => new PoolWorker(script, workerOpt)); | ||
break; | ||
@@ -62,0 +74,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22049
452