npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fork-list

v0.0.9

Published

Easy to fork a list of child process

Downloads

11

Readme

Fork List NPM Version

It's easy to fork a list of child process for node.js by ForkList.

Install

npm install fork-list

Quick Examples

var ForkList = require('fork-list');

// which script to run by multiprocess
var path = './script/write';

// child process num
var num = 3;

var forks = new ForkList({
    path: path,
    num: num
});

for (var i = 0; i < 10; i++) {
    forks.send('hello~', i);
}

forks.shutdown();

the ./script/write.js is:

var Forks = require('fork-list');

Forks.proc(function(data1, data2) {
    console.log('Work id:', this.workid, 'recv data1:', data1, 'data2:', data2);
});

Output:

Work id: 1 recv data1: hello~ data2: 1
Work id: 2 recv data1: hello~ data2: 0
Work id: 0 recv data1: hello~ data2: 5
Work id: 1 recv data1: hello~ data2: 2
Work id: 2 recv data1: hello~ data2: 3
Work id: 1 recv data1: hello~ data2: 9
Work id: 2 recv data1: hello~ data2: 4
Work id: 0 recv data1: hello~ data2: 6
Work id: 2 recv data1: hello~ data2: 7
Work id: 2 recv data1: hello~ data2: 8

Documentation

Initialization

Transfer

Control

Event

fork a new process with specify path.

get total number of processes.

set special classifier.

enable or disable fork-list debug log, or set your special logger such as log4js.getLogger.

Example

var ForkList = require('fork-list');
var underscore = require('underscore');

var times = 10;
var forks = new ForkList();

forks.new('./script');
forks.new('./script');

forks.setClassifier(function(msg, done) {
    var id = underscore.random(0, forks.count() - 1);
    done(null, id);
});

for (var i = 0; i < times; i++) {
    forks.send(i, 'some msg');
}

forks.shutdown();
var forkList = require('fork-list');

// which script to run by multiprocess
var path = './test';

// child process num
var num = 2;

var forks = new ForkList({
    path: path,
    num: num
});

// send data to child process
forks.send(somedata, ...);

The usual data include:

  • Number
  • String
  • Array
  • JSON
  • Object*

Caution The Object data wouldn't complete delivery:

var forks = new ForkList({
    path: path
});

function test(name) {
    this.name = name || 'default';
}

test.prototype.hi = function() {
    console.log('my name is ', this.name);
};

for (var i = 0; i < times; i++) {
    var t = new test('Alan' + i);
    forks.send(i, t);
}

you can only get the basic data { name: 'AlanX' }, and the prototype will lost, you can't call .hi in the child process.

Socket forward exmaple

server.js

var net = require('net');
var config = require('./my_config');
var ForkList = require("fork-list");

var num = 2;
var path = './worker';

var wokers = new ForkList({
    path: path,
    num: num
});

var server = net.createServer();

server.on('connection', function(sock) {

    wokers.foward('socket', sock);

    sock.on('error', function(e) {
        console.log('[server] Error:', e);
    });
});

server.listen(config.port);

console.log('test socket server start');

worker.js

var ForkList = require('fork-list');

ForkList.proc(function(sock) {
    var workid = this.workid;
    sock.on('data', function(data) {
        console.log('[worker] id:', workid, 'data:', data.toString());
    })
});

test client.js

var config = require('./my_config');
var net = require('net');

var client = net.connect({
    hostname: config.host,
    port: config.port
});

client.on('connect', function() {
    var str = 'hello ';
    for (var i = 0; i < 100; i++) {
        client.write(str + i + ' ');
    }
    client.end();
});

client.on('end', function() {
    console.log('client send over!');
});
var forkList = require('fork-list');

forkList.proc(function(msg, ...) {
    /* code */
});

This will forcely kill special child process, and don't care if there are some jobs haven't done.

This will forcely kill special child process, and don't care if there are some jobs haven't done.

This will forcely shutdown all child process, and don't care if there are some jobs haven't done. and once all of child process has exited, the finish event will emit.

Example:

var ForkList = require('fork-list');

var path = './script';
var num = 3;

var forks = new ForkList({
    path: path,
    num: num,
    log: true
});

for (var i = 10; i >= 0; i--) {
    forks.send('hello ' + i);
};

forks.on('error', function(err, pid) {
    console.log('--> Error:', err.message, 'pid:', pid);
});

forks.on('exit', function(pid) {
    console.log('--> Child process exit, pid:', pid);
    forks.killByPid(pid);
});

forks.on('finish', function() {
    console.log('--> All of child process has exited');
});

forks.shutdown();

script.js

var Forks = require('fork-list');

Forks.proc(function(data1, data2) {
    console.log('Work id:', this.workid, 'recv data1:', data1);
});

Output:

Work id: 0 recv data1: hello 10
Work id: 0 recv data1: hello 8
Work id: 0 recv data1: hello 7
Work id: 2 recv data1: hello 9
Work id: 0 recv data1: hello 6
Work id: 1 recv data1: hello 5
Work id: 0 recv data1: hello 2
Work id: 0 recv data1: hello 0
Work id: 1 recv data1: hello 4
Work id: 1 recv data1: hello 3
Work id: 1 recv data1: hello 1
--> Child process exit, pid: 15064
--> Error: IPC channel is already disconnected pid: 15064
--> Child process exit, pid: 13072
--> Error: IPC channel is already disconnected pid: 13072
--> Child process exit, pid: 4900
--> Error: IPC channel is already disconnected pid: 4900
--> All of child process has exited

Test

you can test this module by:

cd node_modules/fork-list
npm test

license

MIT