| 1 | import { dirname, join } from 'path' |
| 2 | import { fileURLToPath } from 'url' |
| 3 | import type { MyData, MyResponse } from './worker' |
| 4 | import { |
| 5 | DynamicThreadPool, |
| 6 | FixedThreadPool, |
| 7 | availableParallelism |
| 8 | } from 'poolifier' |
| 9 | |
| 10 | export const fixedPool = new FixedThreadPool<MyData, Promise<MyResponse>>( |
| 11 | availableParallelism(), |
| 12 | join(dirname(fileURLToPath(import.meta.url)), 'worker.js'), |
| 13 | { |
| 14 | errorHandler: (e: Error) => { |
| 15 | console.error(e) |
| 16 | }, |
| 17 | onlineHandler: () => { |
| 18 | console.info('Worker is online') |
| 19 | } |
| 20 | } |
| 21 | ) |
| 22 | |
| 23 | export const dynamicPool = new DynamicThreadPool<MyData, Promise<MyResponse>>( |
| 24 | Math.floor(availableParallelism() / 2), |
| 25 | availableParallelism(), |
| 26 | join(dirname(fileURLToPath(import.meta.url)), 'worker.js'), |
| 27 | { |
| 28 | errorHandler: (e: Error) => { |
| 29 | console.error(e) |
| 30 | }, |
| 31 | onlineHandler: () => { |
| 32 | console.info('Worker is online') |
| 33 | } |
| 34 | } |
| 35 | ) |