Thread util in Node.js. It's only test on node 12+.
yarn add @yunnysunny/threads
const {NoneBlockingThreadPool: ThreadPool} = require('@yunnysunny/threads');
const poolStr = new ThreadPool({
poolSize: 5,
script: path.join(__dirname, '../scripts/str_worker.js')
});
poolStr.send('test');
poolStr.on(ThreadPool.EVENT_NEW_MESSAGE, function(msg) {
//process msg
});
poolStr.on(ThreadPool.EVENT_WORKER_ERROR, function(err, threadId) {
//show error message
});
const {NoneBlockingThreadPool: ThreadPool} = require('@yunnysunny/threads');
const POOL_SIZE = 5;
const poolStr = new ThreadPool({
poolSize: POOL_SIZE,
script: path.join(__dirname, '../scripts/str_worker.js')
});
poolStr.send({data: 'xx', callback: function(err, str) {
//todo next process
}});
poolStr.on(ThreadPool.EVENT_WORKER_ERROR, function(err, threadId) {
//show error message
});