X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=examples%2Ftypescript%2Fpool.ts;h=1be0691a7aa7bf42423530d25d82994e4ce7fa6e;hb=97bb99ac1e28c87f59b75ea23a9c6643a8fba9c4;hp=869e62a8bf718020c155ae221b85921735a61d74;hpb=b99a3b7852074303d5e17781080235f30d68e6b6;p=poolifier.git diff --git a/examples/typescript/pool.ts b/examples/typescript/pool.ts index 869e62a8..1be0691a 100644 --- a/examples/typescript/pool.ts +++ b/examples/typescript/pool.ts @@ -1,30 +1,52 @@ -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' -export const fixedPool = new FixedThreadPool>( - 8, - join(__dirname, 'worker.js'), +import { + availableParallelism, + DynamicThreadPool, + FixedThreadPool, +} from 'poolifier' + +import type { MyData, MyResponse } from './worker.js' + +const workerFile = join( + dirname(fileURLToPath(import.meta.url)), + `worker${extname(fileURLToPath(import.meta.url))}` +) + +const fixedPool = new FixedThreadPool( + availableParallelism(), + workerFile, { + onlineHandler: () => { + console.info('Worker is online') + }, errorHandler: (e: Error) => { console.error(e) }, - onlineHandler: () => { - console.info('Worker is online') - } } ) -export const dynamicPool = new DynamicThreadPool>( - 2, - 8, - join(__dirname, 'worker.js'), +await fixedPool.execute() + +const dynamicPool = new DynamicThreadPool( + Math.floor(availableParallelism() / 2), + availableParallelism(), + workerFile, { + onlineHandler: () => { + console.info('Worker is online') + }, errorHandler: (e: Error) => { console.error(e) }, - onlineHandler: () => { - console.info('Worker is online') - } } ) + +await dynamicPool.execute() + +// eslint-disable-next-line @typescript-eslint/no-misused-promises +setTimeout(async () => { + await fixedPool.destroy() + await dynamicPool.destroy() +}, 3000)