Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
- provided a non required param cpusRespective.
Browse files Browse the repository at this point in the history
Code written in this commit will fork process till cpus number is not exceeded, than process will wait till some worker finish its operation and than it push new task to queue. Thus optimal execution balance is provided.
  • Loading branch information
IvanProdaiko94 committed Oct 16, 2017
1 parent ab4d3e2 commit a7fb9e1
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
.idea/
61 changes: 43 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,58 @@

var spawn = require('child_process').spawn;

var gulpMultiProcess = function(tasks, cb) {
var completed = 0;
var gulpMultiProcess = function(tasks, cb, cpusRespective) {
var code = 0;

tasks.forEach(function(taskName) {
var args = [process.argv[1], taskName];

process.argv.forEach(function (val) {
if(val[0] === '-' && val !== '--gulpfile') {
args.push(val);
}
});

var worker = spawn(process.execPath, args , { stdio: 'inherit' });

worker.on('exit', function (workerCode) {
var completed;
var each;
var cpusNumber;
var q;
var createWorker = function(onExit, taskName) {
var args = [process.argv[1], taskName];
var worker;
process.argv.forEach(function (val) {
if(val[0] === '-' && val !== '--gulpfile') {
args.push(val);
}
});
worker = spawn(process.execPath, args , { stdio: 'inherit' });
worker.on('exit', onExit);
};

if (!cpusRespective) {
completed = 0;
each = createWorker.bind(this, function (workerCode) {
if(workerCode !== 0) {
code = workerCode;
}

completed++;

if(completed === tasks.length) {
cb(code);
}
});
});
tasks.forEach(each);
} else {
cpusNumber = require('os').cpus().length;
q = require('async.queue');
q = q(function (taskName, callback) {
createWorker(
function (workerCode) {
if(workerCode !== 0) {
code = workerCode;
}
callback();
},
taskName
);
}, cpusNumber);
tasks.forEach(function (task) {
q.push(task)
});

q.drain = function () {
cb(code);
}
}
};

module.exports = gulpMultiProcess;
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gulp-multi-process",
"version": "1.2.0",
"version": "1.3.0",
"homepage": "http://github.com/juanfran/gulp-multi-process",
"repository": "git://github.com/juanfran/gulp-multi-process.git",
"author": {
Expand All @@ -17,12 +17,19 @@
"performance",
"child-process",
"multi-process",
"threads"
"threads",
"spawn",
"fork",
"gulp",
"parallel"
],
"license": "MIT",
"devDependencies": {
"chai": "^3.4.1",
"gulp": "^3.9.0",
"mocha": "^2.3.4"
},
"dependencies": {
"async.queue": "^0.5.2"
}
}
5 changes: 5 additions & 0 deletions test/gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';
var gulpMultiProcess = require('../index.js');
var gulp = require('gulp');

Expand All @@ -22,3 +23,7 @@ gulp.task('task3', function(cb) {
gulp.task('multi', function(cb) {
return gulpMultiProcess(['task1', 'task2', 'task3'], cb);
});

gulp.task('multi-cpus', function (cb) {
return gulpMultiProcess(['task1', 'task2', 'task3'], cb, true);
});
12 changes: 11 additions & 1 deletion test/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';
var exec = require('child_process').exec;
var path = require('path');
var chai = require('chai');
Expand All @@ -10,8 +11,17 @@ describe('gulp-multi-process', function() {
expect(out).to.be.match(/Finished 'task2'/);
expect(out).to.be.match(/Finished 'task3'/);
expect(out).to.be.match(/Finished 'multi'/);

done();
});
});

it('takes into account cpus number', function (done) {
exec('gulp multi-cpus --gulpfile ' + path.join(__dirname, 'gulpfile.js'), function (err, out) {
expect(out).to.be.match(/Finished 'task1'/);
expect(out).to.be.match(/Finished 'task2'/);
expect(out).to.be.match(/Finished 'task3'/);
expect(out).to.be.match(/Finished 'multi-cpus'/);
done();
});
})
});

0 comments on commit a7fb9e1

Please sign in to comment.