Easy multiprocessing in nodejs
About
node-multiprocess
makes multiprocessing in nodejs simple. Sometimes there are CPU bound tasks that are required in nodejs, node-multiprocess
allows a node program to run concurrently to keep IO moving on the main thread.
Install
$ npm i node-multiprocess$ # or $ yarn add node-multiprocess
Usage
// node-multiprocess exports a singleton and a class to be used// the default export is the singleton class;// or; // a regular class can be imported like this;
Singleton usage
There will only ever be one instance of the singleton class
// Before getting first instance of singleton class, you can set the number of processes SingletonPool.setMaxProcesses4; // default is the number of CPUs on machine// The timeout can also be adjusted in secondsSingletonPool.setTimeout120; // default is 60 seconds ;...; // same instance as above
Class usage
There can be many instances of the Pool
class, however, it is not recomended to have more than one at a time because this can cause performance issues
// Number of process can be set either before instantiation or during// before Pool.MAX_PROCESSES = 4; // default is the number of CPUs// during; // both create a pool with four processes// The timeout can also be adjusted before instantiationPool.TIMEOUT = 120; // default is 60 seconds;// ^ Pool will have four processes and a timeout of 120 seconds// create `secondPool` before killing `pool` will cause their to be four additional worker processes alive
Adding jobs
Jobs can be aded by calling the addJob
method. This method returns a promise of the resulting type from the function that is passed to the method
; for ; i < 6; i++ ;promisesResults.forEachconsole.logresult;// 1 2 3 5 8
The addJob
method can accepts any number of arguments and types
pool.addJobaddThree, 10, 15, 20;// Promise<number> => 45 pool.addJobmergeObj, , ;// Promise<Object> => {a: 'a', b: 'b'}
Killing process pool
When finished with process pool and all jobs are complete use the kill
method to destroy all process
pool.kill; // sends SIGINT to all child processes