X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=examples%2Ftypescript%2Fpool.ts;h=fb48f7b07db76324a75afc001e131dd448a6809f;hb=refs%2Ftags%2Fv3.1.28;hp=93774422b4500459715ec236c136c1601f13add4;hpb=9bc1ca7575fb508018f641dfee8c9939beda517a;p=poolifier.git diff --git a/examples/typescript/pool.ts b/examples/typescript/pool.ts index 93774422..fb48f7b0 100644 --- a/examples/typescript/pool.ts +++ b/examples/typescript/pool.ts @@ -1,18 +1,20 @@ -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.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, { @@ -25,7 +27,9 @@ export const fixedPool = new FixedThreadPool>( } ) -export const dynamicPool = new DynamicThreadPool>( +await fixedPool.execute() + +const dynamicPool = new DynamicThreadPool( Math.floor(availableParallelism() / 2), availableParallelism(), workerFile, @@ -38,3 +42,11 @@ export const dynamicPool = new DynamicThreadPool>( } } ) + +await dynamicPool.execute() + +// eslint-disable-next-line @typescript-eslint/no-misused-promises +setTimeout(async () => { + await fixedPool.destroy() + await dynamicPool.destroy() +}, 3000)