Skip to content

Commit

Permalink
fix: calculate number of workers correctly (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic authored and evilebottnawi committed Dec 17, 2018
1 parent 1f81d04 commit fcbd813
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -61,7 +61,8 @@ use: [
loader: "thread-loader",
// loaders with equal options will share worker pools
options: {
// the number of spawned workers, defaults to number of cpus
// the number of spawned workers, defaults to (number of cpus - 1) or
// fallback to 1 when require('os').cpus() is undefined
workers: 2,

// number of jobs a worker processes in parallel
Expand Down
11 changes: 10 additions & 1 deletion src/workerPools.js
Expand Up @@ -3,10 +3,19 @@ import WorkerPool from './WorkerPool';

const workerPools = Object.create(null);

function calculateNumberOfWorkers() {
// There are situations when this call will return undefined so
// we are fallback here to 1.
// More info on: https://github.com/nodejs/node/issues/19022
const cpus = os.cpus() || { length: 1 };

return Math.max(1, cpus.length - 1);
}

function getPool(options) {
const workerPoolOptions = {
name: options.name || '',
numberOfWorkers: options.workers || os.cpus().length,
numberOfWorkers: options.workers || calculateNumberOfWorkers(),
workerNodeArgs: options.workerNodeArgs,
workerParallelJobs: options.workerParallelJobs || 20,
poolTimeout: options.poolTimeout || 500,
Expand Down

0 comments on commit fcbd813

Please sign in to comment.