Basic usage with node:worker_threads
mainBy default, Tinypool uses node:worker_threads. You initialize a pool by providing a filename pointing to your worker script. You then use pool.run(data) to execute tasks. Always remember to call await pool.destroy() when the pool is no longer needed to terminate idle workers and free resources.
// main.mjs
import Tinypool from 'tinypool'
const pool = new Tinypool({
filename: new URL('./worker.mjs', import.meta.url).href,
})
const result = await pool.run({ a: 4, b: 6 })
console.log(result) // Prints 10
// Make sure to destroy pool once it's not needed anymore
// This terminates all pool's idle workers
await pool.destroy()// worker.mjs
export default ({ a, b }) => {
return a + b
}