safe-instances
v2.0.2
Published
A complete, functional, fast, safe and simple Node.js child processing module.
Downloads
8
Maintainers
Readme
safe-instances (previously called safe_children
)
A complete, functional, fast and simple Node.js threading module.
safe-instances
was developed by Oratio.io for Oratio.js's Module System.
It's based on children pools, allowing you to
NOTE: With release 2.0, sending file names when doing new Child()
is DEPRECATED. You must do new ChildFile()
, as explained in the quick-start tutorial.
Installation
npm install safe-instances
Getting Started
var Child = require('safe-instances');
var pool = new Child.Pool(3); //Creates pool with 3 processes
var child = new Child('process.handle("randomMessage", function(value, callback){ callback(value) }', pool, 3 * 60);
child.start();
child.contact('randomMessage', 'myValue') //NOTE: You might pass a third callback argument, too.
.then(function(value){
console.log(value); //<- "myValue"
});
Need to send file locations instead of strings when creating new script? Do:
Child.File.usesCache = false; //Default: true. Caches scripts for being re-used.
var child = new Child.File(__dirname + 'script.js', 3 * 60);
[...]
Documentation
new Child(code, pool, timeout)
Returns new Child object with following properties:
- child.setPool(pool); Sets child's pool to pool.
- child.encoding - Default:
utf8
. - child.commandType - Default:
node
. - child.timeout - The timeout you set when creating a child - defaults to 1 minute. Only works before beginning the script.
- child.logs - Do
console.log
s from the child appear on your console? - child.contact(event, message) - Returns
Promise
. Should be handled withprocess.handle
in the child process.