forked from 418sec/curlrequest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spawn.js
33 lines (28 loc) · 752 Bytes
/
spawn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
var child = require('child_process');
/**
* Limit the amount of processes that can be spawned per tick.
*/
var spawned = 0
, max_per_tick = 10
, resetting = false;
/**
* See `child_process.spawn()`.
*/
module.exports = function (cmd, args, options, callback) {
var args = Array.prototype.slice.call(arguments);
if (spawned < max_per_tick) {
spawned++;
callback(child.spawn.apply(child, args.slice(0, -1)));
} else {
if (!resetting) {
resetting = true;
process.nextTick(function () {
spawned = 0;
resetting = false;
});
}
process.nextTick(function () {
module.exports.apply(null, args);
});
}
};