Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workerfarm cleanup #1918

Merged
merged 2 commits into from Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Parser.js
Expand Up @@ -83,4 +83,4 @@ class Parser {
}
}

module.exports = Parser;
module.exports = Parser;
11 changes: 11 additions & 0 deletions src/utils/cpuCount.js
@@ -0,0 +1,11 @@
const os = require('os');

module.exports = function() {
let cores;
try {
cores = require('physical-cpu-count');
} catch (err) {
cores = os.cpus().length;
}
return cores || 1;
};
2 changes: 1 addition & 1 deletion src/workerfarm/Worker.js
Expand Up @@ -41,7 +41,7 @@ class Worker extends EventEmitter {

this.child = childProcess.fork(childModule, process.argv, options);

this.child.on('message', this.receive.bind(this));
this.child.on('message', data => this.receive(data));

this.child.once('exit', code => {
this.exitCode = code;
Expand Down
22 changes: 7 additions & 15 deletions src/workerfarm/WorkerFarm.js
@@ -1,7 +1,7 @@
const {EventEmitter} = require('events');
const os = require('os');
const errorUtils = require('./errorUtils');
const Worker = require('./Worker');
const cpuCount = require('../utils/cpuCount');

let shared = null;
class WorkerFarm extends EventEmitter {
Expand Down Expand Up @@ -60,7 +60,7 @@ class WorkerFarm extends EventEmitter {
}

mkhandle(method) {
return function(...args) {
return (...args) => {
// Child process workers are slow to start (~600ms).
// While we're waiting, just run on the main thread.
// This significantly speeds up startup time.
Expand All @@ -73,7 +73,7 @@ class WorkerFarm extends EventEmitter {

return this.localWorker[method](...args, false);
}
}.bind(this);
};
}

onError(error, worker) {
Expand Down Expand Up @@ -258,17 +258,9 @@ class WorkerFarm extends EventEmitter {
}

static getNumWorkers() {
if (process.env.PARCEL_WORKERS) {
return parseInt(process.env.PARCEL_WORKERS, 10);
}

let cores;
try {
cores = require('physical-cpu-count');
} catch (err) {
cores = os.cpus().length;
}
return cores || 1;
return process.env.PARCEL_WORKERS
? parseInt(process.env.PARCEL_WORKERS, 10)
: cpuCount();
}

static callMaster(request, awaitResponse = true) {
Expand All @@ -285,7 +277,7 @@ class WorkerFarm extends EventEmitter {
}

static getConcurrentCallsPerWorker() {
return parseInt(process.env.PARCEL_MAX_CONCURRENT_CALLS) || 5;
return parseInt(process.env.PARCEL_MAX_CONCURRENT_CALLS, 10) || 5;
}
}

Expand Down