0.0.2 • Published 10 years ago

lemmings v0.0.2

Weekly downloads
1
License
-
Repository
-
Last release
10 years ago

lemmings

A small process queuing manager

Extracted from music.json

Why?

Initially used to limit the number of child processes spawned on async exec calls. Spawning too many child processes can lead to Error: spawn EMFILE which is no good on massively asynchronous script (music.json in my case).

Usage

  $ npm install lemmings
  // lemming_example.js

  var Lemmings = require('./lemmings');

  // Initialize and set max concurrent processes to 2
  var lemmings = new Lemmings(8);

  // Declare function to be called when all jobs completed
  lemmings.done = function(){
    console.log("\nLemmings have completed the level");
  };

  // Random Async Task
  function task(name){
    exec("sleep "+Math.random(), function(){
      console.log(name+'...');
      lemmings.next(); // call next lemming on async completion
    });
  };

  // Run various tasks
  for(var i = 0; i < 5; i++){
    // Input function into Lemmings#try
    lemmings.try(task.bind(null,'Digging'));
    lemmings.try(task.bind(null,'Building steps'));
    lemmings.try(task.bind(null,'Parachuting'));
  };

Results (may vary)

  $ node lemming_example.js
  Digging...
  Parachuting...
  Digging...
  Building steps...
  Parachuting...
  Building steps...
  Digging...
  Digging...
  Parachuting...
  Digging...
  Parachuting...
  Building steps...
  Building steps...
  Parachuting...
  Building steps...

  Lemmings have completed the level.

Notes

  • Assumes only one async call per Lemmings#try i.e. function passed into Lemmings#try should only contain one async call (fixed in future updates)
  • Need to add Gruntfile

License

Lemmings is released under the MIT License.