X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=examples%2Ftypescript%2Fpool.ts;h=3965e1f0a1cd4c8e2632b59018972e79fa5b798e;hb=15f12f77ca67758e32afdf2c5658075d6beac912;hp=f85496434c2516eea46c7b5f1cde937565a2985d;hpb=6f65a0015850a7a5821cea00eb6d24cf046e5f19;p=poolifier.git diff --git a/examples/typescript/pool.ts b/examples/typescript/pool.ts index f8549643..3965e1f0 100644 --- a/examples/typescript/pool.ts +++ b/examples/typescript/pool.ts @@ -1,41 +1,52 @@ -import { dirname, extname, join } from 'path' -import { fileURLToPath } from 'url' -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' -import type { MyData, MyResponse } from './worker' + +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>( +const fixedPool = new FixedThreadPool( availableParallelism(), workerFile, { - errorHandler: (e: Error) => { - console.error(e) - }, onlineHandler: () => { console.info('Worker is online') + }, + errorHandler: (e: Error) => { + console.error(e) } } ) -export const dynamicPool = new DynamicThreadPool>( +await fixedPool.execute() + +const dynamicPool = new DynamicThreadPool( Math.floor(availableParallelism() / 2), availableParallelism(), workerFile, { - errorHandler: (e: Error) => { - console.error(e) - }, onlineHandler: () => { console.info('Worker is online') + }, + errorHandler: (e: Error) => { + console.error(e) } } ) + +await dynamicPool.execute() + +// eslint-disable-next-line @typescript-eslint/no-misused-promises +setTimeout(async () => { + await fixedPool.destroy() + await dynamicPool.destroy() +}, 3000)