X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=examples%2Ftypescript%2Fpool.ts;h=1be0691a7aa7bf42423530d25d82994e4ce7fa6e;hb=HEAD;hp=3d9d9e6f482723cdb634e98bd02a9bbd46df3314;hpb=aed302d456407817a368e3f743020474b60facd2;p=poolifier.git diff --git a/examples/typescript/pool.ts b/examples/typescript/pool.ts index 3d9d9e6f..c339b705 100644 --- a/examples/typescript/pool.ts +++ b/examples/typescript/pool.ts @@ -1,18 +1,19 @@ import { dirname, extname, join } from 'node:path' import { fileURLToPath } from 'node:url' -import type { MyData, MyResponse } from './worker' import { + availableParallelism, DynamicThreadPool, FixedThreadPool, - availableParallelism } from 'poolifier' +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, { @@ -21,11 +22,13 @@ export const fixedPool = new FixedThreadPool>( }, onlineHandler: () => { console.info('Worker is online') - } + }, } ) -export const dynamicPool = new DynamicThreadPool>( +await fixedPool.execute() + +const dynamicPool = new DynamicThreadPool( Math.floor(availableParallelism() / 2), availableParallelism(), workerFile, @@ -35,6 +38,14 @@ export const dynamicPool = new DynamicThreadPool>( }, 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)