X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=examples%2Ftypescript%2Fpool.ts;h=144d55002bd048ea865365cb6696cdb887745230;hb=e444b0ffc5743092b3e4c650f56cc40ed74622f8;hp=869e62a8bf718020c155ae221b85921735a61d74;hpb=b99a3b7852074303d5e17781080235f30d68e6b6;p=poolifier.git diff --git a/examples/typescript/pool.ts b/examples/typescript/pool.ts index 869e62a8..144d5500 100644 --- a/examples/typescript/pool.ts +++ b/examples/typescript/pool.ts @@ -1,10 +1,20 @@ -import { join } from 'path' -import type { MyData, MyResponse } from './worker' -import { DynamicThreadPool, FixedThreadPool } from 'poolifier' +import { dirname, extname, join } from 'node:path' +import { fileURLToPath } from 'node:url' +import type { MyData, MyResponse } from './worker.js' +import { + DynamicThreadPool, + FixedThreadPool, + availableParallelism +} from 'poolifier' -export const fixedPool = new FixedThreadPool>( - 8, - join(__dirname, 'worker.js'), +const workerFile = join( + dirname(fileURLToPath(import.meta.url)), + `worker${extname(fileURLToPath(import.meta.url))}` +) + +export const fixedPool = new FixedThreadPool( + availableParallelism(), + workerFile, { errorHandler: (e: Error) => { console.error(e) @@ -15,10 +25,10 @@ export const fixedPool = new FixedThreadPool>( } ) -export const dynamicPool = new DynamicThreadPool>( - 2, - 8, - join(__dirname, 'worker.js'), +export const dynamicPool = new DynamicThreadPool( + Math.floor(availableParallelism() / 2), + availableParallelism(), + workerFile, { errorHandler: (e: Error) => { console.error(e) @@ -28,3 +38,9 @@ export const dynamicPool = new DynamicThreadPool>( } } ) + +// eslint-disable-next-line @typescript-eslint/no-misused-promises +setTimeout(async () => { + await fixedPool.destroy() + await dynamicPool.destroy() +}, 3000)