Giter VIP home page Giter VIP logo

node-worker-pool's Introduction

node-worker-pool Build Status

node-worker-pool is a library for managing a pool of child workers in node.

It's primarily useful for scenarios where you have lots of highly parallelizable tasks you want to perform. It works exclusively via message-passing, so there is no need to share memory.

Specifically, node-worker-pool allows you to define your own worker executable that is capable of communicating over stdin/stdout (via a fairly simple protocol for which I have yet to propertly document :p).

Getting started

  • Write a worker executable file
  • Construct a WorkerPool object that points at the aforementioend worker executable
  • Send messages to the WorkerPool object and wait for responses

Writing a worker executable file

You technically don't have to write this file in node, but for the time being there are only node helper libraries for abstracting away the communciation protocols. Here is an example worker:

worker.js

var workerUtils = require('node-worker-pool/nodeWorkerUtils');

/**
 * Executed once when the worker pool first starts
 * (before any messages are received)
 */
var initData;
function onInitialize(data) {
  initData = data;
}

/**
 * Executed each time a message is received from the worker pool.
 * Returns the response to the message (response must always be an object)
 */
function onMessage(data) {
  return {
    initData: initData,
    receivedData: data
  };
}

if (require.main === module) {
  try {
    workerUtils.startWorker(onInitialize, onMessage);
  } catch (e) {
    workerUtils.respondWithError(e);
  }
}

workerPool.js

if (require.main === module) {
  var workerPool = new WorkerPool(
    8,                // number of workers
    process.execPath, // path to the node binary
    './worker.js',    // path to the worker script
    {
      // The initData object that is passed to each worker exactly once before
      // any messages get sent. Workers receive this object via their
      // onInitialize callback.
      initData: {someUsefulConstant: 42}
    }
  );

  workerPool.sendMessage({message: 'hai!'}).then(function(response) {
    console.log(response); // Prints the response object from the worker
  });

  workerPool.shutDown().then(function() {
    console.log('All worker processes have now been killed');
  });
}

node-worker-pool's People

Contributors

cpojer avatar jcreedcmu avatar jeffmo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.