X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=examples%2Ftypescript%2Fpool.ts;h=c55c4e96557dc8fcb400847745b0ac2c5ee014c7;hb=ea4b5fd037c5fe09eb6a2810332ad6bed1b5bc7f;hp=54b437ab31b2f1d7785439c5ce9007167e554331;hpb=31a7d5bea6971eff8079efd2a164a66086f6d4b3;p=poolifier.git diff --git a/examples/typescript/pool.ts b/examples/typescript/pool.ts index 54b437ab..c55c4e96 100644 --- a/examples/typescript/pool.ts +++ b/examples/typescript/pool.ts @@ -1,14 +1,22 @@ -import { join } from 'path' -import type { MyData, MyResponse } from './worker' +import { dirname, extname, join } from 'node:path' +import { fileURLToPath } from 'node:url' + import { + availableParallelism, DynamicThreadPool, - FixedThreadPool, - availableParallelism + FixedThreadPool } from 'poolifier' -export const fixedPool = new FixedThreadPool>( +import type { MyData, MyResponse } from './worker.js' + +const workerFile = join( + dirname(fileURLToPath(import.meta.url)), + `worker${extname(fileURLToPath(import.meta.url))}` +) + +export const fixedPool = new FixedThreadPool( availableParallelism(), - join(__dirname, 'worker.js'), + workerFile, { errorHandler: (e: Error) => { console.error(e) @@ -19,10 +27,10 @@ export const fixedPool = new FixedThreadPool>( } ) -export const dynamicPool = new DynamicThreadPool>( +export const dynamicPool = new DynamicThreadPool( Math.floor(availableParallelism() / 2), availableParallelism(), - join(__dirname, 'worker.js'), + workerFile, { errorHandler: (e: Error) => { console.error(e) @@ -32,3 +40,9 @@ export const dynamicPool = new DynamicThreadPool>( } } ) + +// eslint-disable-next-line @typescript-eslint/no-misused-promises +setTimeout(async () => { + await fixedPool.destroy() + await dynamicPool.destroy() +}, 3000)