1.0.5 • Published 3 years ago

@gimmi7/ts-worker-pool v1.0.5

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

@gimmi7/ts-worker-pool

Getting Started

install @gimmi7/ts-worker-pool using npm:

npm install @gimmi7/ts-worker-pool --save

Import WorkerPool and run a method with worker_threads pool:

import { WorkerPool } from '@gimmi7/ts-worker-pool';
import { moduleurl } from './task.js';

const workerpool = WorkerPool.defaultPool()

const greeting = await workerpool.run<string>(moduleurl, "saihi", "gimmi7")
console.log(greeting)  // will print "hi,gimmi7"

task.ts

export function saihi(name: string): string {
  return "hi," + name;
}

export const moduleurl = import.meta.url

Rationale

  • run a worker and listen message on default MessagePort

  • a message contains (moduleurl, functionName, funcArgs)

  • import module with moduleurl, then run the function with funcArgs

  • wrap the function run in a promise, return immediate

  • when the function ended, resolve the promise, or reject with error

  • the worker can be reuseed, so we can maintain a woker pool

1.0.2

3 years ago

1.0.1

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.0

3 years ago