rpc-worker-threads
v0.0.8
Published
Worker thread with a RPC api
Downloads
5
Maintainers
Readme
IMPORTANT
This library is not ready to production use, but you can experiment with it on your pet projects 😊
rpc-worker-threads
How to use
const { isMainThread } = require('worker_threads');
if (isMainThread) {
const { RPCWorker } = require('rpc-worker-threads');
async function init() {
const rpcWorker = new RPCWorker(__filename);
await rpcWorker.init();
const result = await rpcWorker.send({
name: 'myFn',
data: 'World',
});
console.log(result); // Hello World!
}
init();
} else {
const { RPCWorker } = global;
RPCWorker.addHandler('myFn', (v) => {
return `Hello ${v}!`;
});
}
API
RPCWorker(filename: string, options?: WorkerOptions | undefined) extends Worker
RPCWorker.send(opts): Promise
- opts.name - remote method name
- opts.data [optional] - payload
- opts.transferList - transfer list. More in the node docs
- opts.executionTimeout [optional] - execution timeout
RPCWorker.setDefaultExecutionTimeout(timeout: number)
RPC
PRC.addHandler(fnName: string, callback: Function)
RPCWorkersPool(poolOpts, filename: string, options?: WorkerOptions | undefined)
RPCWorkersPool.send(opts): Promise
- opts.name - remote method name
- opts.data [optional] - payload
- opts.transferList - transfer list. More in the node docs
- opts.executionTimeout [optional] - execution timeout
RPCWorkersPool.close(): Promise
Custom pooling example
Look example here