forkme
Spawn a process without a module
Last updated 3 years ago by lousando .
MIT · Repository · Original npm · Tarball · package.json
$ cnpm install forkme 
SYNC missed versions from official npm registry.

forkme

Travis branch

Spawn a process without a module

(does NOT write any temporary files)

Installation

yarn add forkme

Usage

let forkme = require('forkme');

Basic Use

var child = forkme(function () {
    console.log("running in a child process!");
});

Context

A fake copy of the calling module is created in the child process, so as much as possible the closure should behave as if it's still in the module that called forkme().

One possible way in which the calling module and the closure module might differ, is that the closure module will be the main module of the child process.

forkme(function () {
	console.log(require.main === module); // true
});

require
module.require

Modules required from the closure should resolve the same way they would if they had been required in the calling module.

Given:

  • Calling module "/path/to/calling-module.js"
  • Other module "/path/to/node_modules/foo/index.js"
  • Other module "/path/to/bar.js"

Then:

forkme(function () {
	console.log(require.resolve('foo')); // "/path/to/node_modules/foo/index.js"
	console.log(require.resolve('./bar')); // "/path/to/bar.js"
});

__filename
__dirname
module.filename

These variables have the same values in the closure as they did in the calling module.

module.id

This will be "." because the closure is the main module of the child process.

Arguments

An optional array of values can be passed as the first argument. The values will be passed to the closure when it's called in the child process.

forkme(['foo', 'bar'], function(a, b) {
    console.log(a + " " + b); // "foo bar"
});

All values in the arguments array must be JSON serializable.

Options

Environment variables, working directory, input and output streams, etc., can be modified by passing an options object. The options are passed directly to child_process.fork().

var child = forkme({
    cwd: "/some/path",
    env: { foo: 'bar' },
    silent: true // Pipe stdin, stdout, and stderr to parent process.
}, function () {
    console.log(process.cwd() + ", " + process.env.foo);
});

child.stdout.setEncoding('utf8');
child.stdout.on('data', function (data) {
	process.stdout.write(data); // "/some/path, bar";
});

Return

If the closure returns a defined value, an IPC message will be sent back to the parent process with the returned value. The return value should be JSON serializable.

var child = forkme(function () {
	return 'foo';
});

child.on('message', function (message) {
	console.log(message); // "foo"
});

API

forkme(closure)
forkme(args, closure)
forkme(options, closure)
forkme(args, options, closure)

  • args Array (optional)
  • options Object (optional)
  • closure Function
  • returns ChildProcess

Spawns a child process which calls the closure, passing it all values in the args array.

The options argument is passed directly to child_process.fork(). See the documentation for child_process.fork() for more information.

A standard Node.js ChildProcess instance is returned.

Current Tags

  • 2.0.1                                ...           latest (3 years ago)

11 Versions

  • 2.0.1                                ...           3 years ago
  • 2.0.0 [deprecated]           ...           3 years ago
  • 1.0.8 [deprecated]           ...           6 years ago
  • 1.0.7 [deprecated]           ...           6 years ago
  • 1.0.6 [deprecated]           ...           6 years ago
  • 1.0.5 [deprecated]           ...           6 years ago
  • 1.0.4 [deprecated]           ...           6 years ago
  • 1.0.3 [deprecated]           ...           6 years ago
  • 1.0.2 [deprecated]           ...           6 years ago
  • 1.0.1 [deprecated]           ...           6 years ago
  • 1.0.0 [deprecated]           ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (2)
Dependents (2)

Copyright 2014 - 2017 © taobao.org |