Forkify lets you run functions in a forked child process
$ npm install forkify
- Distribute executions over multiple instances
- Create new instances when existing ones are too busy
- Buffers & streams are sent over a separate socket to bypass costly JSON serialization
- Idle instances are reaped (by default after 30 seconds)
- Events can be emitted from the fork
- You should also be able to send events to the fork
- Emitted event arguments should be serialized through the same process as a function execution or callback, so buffers & streams can also be emitted
Forkify a function
var forkify = ;// Forkify the functionvar fibonacci =;;
Work with buffers
You can pass Buffer instances back and forth, without problems. These are not serialized using JSON, as that is a costly affair. Unfortunately, they are also not using "shared memory", rather the buffer contents are sent to the child over an extra stream.
var workWithBuffers =;;
Set instance pool size
By default, up to 3 instances are created. This can be modified at any time like this:
forkifylimit = 5;
Create a new forkify pool
If you want to create a separate pool, you can do so like this:
var forkify2 = forkifyconstructor;
Set minimum idle time before reaping
Forks are killed after idling for at least 30 seconds.
This can be changed by setting the
forkifyidle = 45000; // 45 seconds
Do note: For now, the reaping function only runs once every 10 seconds.
forkify functions run in a new scope and can not access anything outside of it, you will have to re-
require modules you wish to use inside the function.
Arguments passed to the forkified function are converted using a modified version of JSON-DRY (which supports Dates, Infinity, Errors, Buffers, ...)
Because of the addition of Buffer support
forkify will keep your application running until all instances have been reaped.