0.0.2 • Published 2 years ago
easy-worker-pool v0.0.2
easy-worker-pool
A RPC-like framework for managing webworker pool, you can make full use of cpu resources to do some things, just write your method in a file like "worker.js", and then you can call these methods remotely from the main(UI) thread or other workers, you don't need to worry about the follow-up, the framework will evenly distribute a large number of calls to different webworkers for execution.
How to use
Installation:
$ npm i easy-worker-pool
In main thread
// App.tsx or some entry index.ts
import { WorkerCenter } from 'easy-worker-pool';
import WorkerUrl from './pool.worker.ts';
// import { othersWorker } from './othersWorker-creator.ts';
export let workerMethods: WorkerProxy;
async function initPoolWorker() {
const center = new WorkerCenter();
const proxy = center.init<WorkerProxy>(WorkerUrl, { maxWorkerNumbers: 6 });
const workerMethods = await proxy;
// If you need to use poolworker in other workers, you need to add the following line to establish a connection for them:
await center.bindOtherWorkerAndPoolWorkers(othersWorker);
// now the connection between worker pool and other workers is established
}
initPoolWorker().then(()=>{
// now yopu can call remotely the poolworkers's methods in main thread
workerMethods
.fn1(123, 23132)
// Another usage, you can pass additional parameters:
// .fn1({ methodArgs: [123, 23132], proxyOptions: { timeout: 500 } })
.then(rsp => console.log('pool >> fn1 >> main||rsp', rsp));
.catch(err => console.log('pool >> fn1 >> main||err', err));
// ...do something else
})
In pool-worker file
// pool.worker.ts
import { registerPoolWorker } from 'easy-worker-pool';
export const fn1 = async (ff11: number, asd: number) => {
return ff11 + asd;
};
export const fn3 = (ff333: string) => {
return ff333;
};
export const fn2 = (ff222: boolean) => {
return ff222;
};
const workerProxy = registerPoolWorker({
fn1,
fn2,
fn3,
});
// Use the following types in other files and you will get typescript infer
export type WorkerProxy = typeof workerProxy;
// if you use webpack worker-loader, can export the worker file like:
export default {} as WorkerFile
In other worker file
// others.worker.ts
import { registerOtherWorker } from 'easy-worker-pool';
export let workerMethods: WorkerProxy;
export const initPoolWorker = () => {
registerOtherWorker<WorkerProxy>().then(methods => {
workerMethods = methods;
});
};
initPoolWorker()
// After the initialization(center.bindOtherWorkerAndPoolWorkers) is complete, you can directly use workerMethods to rpc a poolworker's method
// workerMethods
// .fn1(123, 23132)
// .fn1({ methodArgs: [123, 564], proxyOptions: { timeout: 100 } })
// .catch(e => {}));
TypeScript Support
easy-worker-pool is written in TypeScript, of course you can also use it in Javascript, just ignore the TS syntax in the example