dead simple worker child process manager. respawn children. load balance work amongst children.
dead simple worker process manager. respawn children. load balance work amongst children.
there are a some obvious use cases.
Process Send / Manager api. This is useful if you just need to keep a set of child processes running but do not need to send a high volume of data. You may send as much data as you want but know you should pause if send returns false.
var forkfriend = require'friend';var friend = forkfriend;// make 3 somefriend.js worker with argumentsfriendadd'somefriend.js''-a'13;// workers for the same file get the same args as the first.// multiple calls to add add that many more to the poolfriendadd'somefriend.js'// if send returns false you should wait for the drain event before sending more data.friendsend'hey one of you work on this';friendon'message'// message is the data the child sent.// workername === 'somefriend.js'// worker instanceof ChildProcess// when you are all donefriendstop;
If you are going to do any meaningful amount of work you already have a stream. A stream abstraction is essential because the IPC channel WILL get backed up until the process is killed or you run out of memory and everything starts to suck. The manager is a Duplex Stream so just pipe in and pipe out the results. Workers need not send back results. But if they do those will be emitted as message/data events on the manager.
var fs = require'fs';// you will need some sort of transform stream to change the buffers into distinct messages.// the child process manager load balances "messages" ie calls to send()// each data event sent to the manager must be a distinct message in thisvar lineStream = require'line-stream';var forkfriend = require'forkfriend';var manager = forkfriend;// fork 4 processes of worker.js// the messages will be distributed across all 4 processes in a round robin/first available schememanageradd'./worker.js'4;fscreateReadStream'application.log'pipelineStreampipemanagerpipefscreateWriteStream'processed.log';
processon'message'// do somthing// tell friend about itprocesssend'hey i worked onn '+data;;
let me know if this is helpful or if you have any issues.