X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=examples%2Ftypescript%2Fpool.ts;h=57898d98a9d53a8c89c3d774847e9cae694a53fe;hb=0e5402633adaf3af5ce1148c9d4364b54c69d64f;hp=c2d0a1b8d1f1ba004993d820f8dcdef28f3bd7aa;hpb=a974afa67937a18f493d733d9f5285b99c8b08fd;p=poolifier.git diff --git a/examples/typescript/pool.ts b/examples/typescript/pool.ts index c2d0a1b8..57898d98 100644 --- a/examples/typescript/pool.ts +++ b/examples/typescript/pool.ts @@ -1,30 +1,46 @@ -import { join } from 'path' -import { DynamicThreadPool, FixedThreadPool } from 'poolifier' -import type { MyData, MyResponse } from './worker' +import { dirname, extname, join } from 'node:path' +import { fileURLToPath } from 'node:url' +import type { MyData, MyResponse } from './worker.js' +import { + DynamicThreadPool, + FixedThreadPool, + availableParallelism +} from 'poolifier' + +const workerFile = join( + dirname(fileURLToPath(import.meta.url)), + `worker${extname(fileURLToPath(import.meta.url))}` +) export const fixedPool = new FixedThreadPool>( - 8, - join(__dirname, 'worker.js'), + availableParallelism(), + workerFile, { errorHandler: (e: Error) => { console.error(e) }, onlineHandler: () => { - console.log('Worker is online') + console.info('Worker is online') } } ) export const dynamicPool = new DynamicThreadPool>( - 2, - 8, - join(__dirname, 'worker.js'), + Math.floor(availableParallelism() / 2), + availableParallelism(), + workerFile, { errorHandler: (e: Error) => { console.error(e) }, onlineHandler: () => { - console.log('Worker is online') + console.info('Worker is online') } } ) + +// eslint-disable-next-line @typescript-eslint/no-misused-promises +setTimeout(async () => { + await fixedPool.destroy() + await dynamicPool.destroy() +}, 3000)