The signature of your callback function, including its arguments and return type.
Create a new ThreadPool
to group worker threads together and run multiple worker threads in parallel.
The callback function to be executed in parallel upon calling the asynchronous run method.
Argument types must be serializable using the structuredClone() algorithm.
Callback functions can not be closures or rely upon top level imports, as they do not have access to variables or imports outside of their isolated worker thread environment.
They can however use dynamic imports using the const myPackage = await import('some_package')
syntax.
Optional
options: ThreadPoolOptionsReadonly
fnThe callback function to be executed in parallel upon calling the asychronous run method.
The number of Threads in the pool that are currently running tasks. Once this number reaches 0
, it is safe to close the ThreadPool
.
It is possible the check this while a task is still running. Their statuses are stored on the main thread while the tasks are performed on the underlying workers.
To wait until the ThreadPool
is not busy, await the idle property.
import { ThreadPool } from "bun-threads";
const tp = new ThreadPool(() => {
return 'hello world'
})
tp.run([])
console.log(`${tp.busy} thread in the ThreadPool is busy.`)
tp.run([])
console.log(`${tp.busy} threads in the ThreadPool are busy.`)
await tp.idle
console.log(`${tp.busy} threads in the ThreadPool are busy.`)
tp.close()
A promise that resolves once all of the Threads in the ThreadPool
have finished their tasks and reached an idle
state. Resolves immediately if no Thread
s are busy.
Works similar to the Thread.idle property, except that the promise resolves to void.
How long (in milliseconds) to keep the dynamic Threads in the ThreadPool
active after completing a task before terminating them.
minThreads number of Thread
s have their Thread.idleTimeout set to Infinity
. All other threads will have their Thread.idleTimeout
set to this value.
This allows the ThreadPool
to grow and shrink based upon demand, using more or less resources as required.
Default is 0
(close immediately).
Changing this value will restart the ThreadPool
's internal timer.
How long (in milliseconds) to keep the dynamic Threads in the ThreadPool
active after completing a task before terminating them.
minThreads number of Thread
s have their Thread.idleTimeout set to Infinity
. All other threads will have their Thread.idleTimeout
set to this value.
This allows the ThreadPool
to grow and shrink based upon demand, using more or less resources as required.
Default is 0
(close immediately).
Changing this value will restart the ThreadPool
's internal timer.
The maximum number of Threads that are allowed to be running at once. Once this limit is reached, any further calls will be enqueued in a promise until a Thread
becomes available.
By default, the maximum is set to os.availableParallelism() - 1, which generally should not be exceeded, as it should not offer any performance gains.
Setting this value to less than minThreads will cause minThreads
to be lowered to the same value.
The maximum number of Threads that are allowed to be running at once. Once this limit is reached, any further calls will be enqueued in a promise until a Thread
becomes available.
By default, the maximum is set to os.availableParallelism() - 1, which generally should not be exceeded, as it should not offer any performance gains.
Setting this value to less than minThreads will cause minThreads
to be lowered to the same value.
The number of Threads to keep active with a Thread.idleTimeout of Infinity
(never close automatically).
By default, one Thread
will be kept warm for fast startup times.
Additional Thread
s in the range of minThreads
+ 1 and maxThreads (inclusive) will have their Thread.idleTimeout
set to idleTimeout.
Setting this value to greater than maxThreads
will cause maxThreads
to be raised to the same value.
The number of Threads to keep active with a Thread.idleTimeout of Infinity
(never close automatically).
By default, one Thread
will be kept warm for fast startup times.
Additional Thread
s in the range of minThreads
+ 1 and maxThreads (inclusive) will have their Thread.idleTimeout
set to idleTimeout.
Setting this value to greater than maxThreads
will cause maxThreads
to be raised to the same value.
Close all of the Threads in the pool. It is safe to call this method more than once, as subsequent calls result in a no-op.
Optional
force: boolean = falseThis method will wait for the Thread
to finish its queued tasks unless force
is set to true
. Default is false
.
a Promise<number> that resolves to the amount of open threads that were actually closed (how many Thread.close()
calls returned true
).
Execute the callback that was specified in the constructor in a separate worker thread.
An array of arguments to pass to the callback function. If your callback function does not have arguments, you still must pass an empty array. This is required for TypeScript to be able infer arguments. Argument types must be serializable using the structuredClone() algorithm.
A Promise<ReturnType<T>> that resolves to the return type of your callback function.
A pool of available threads that can be called to run a task multiple times in parallel.
The following example demonstrates the benefits of using the ThreadPool class over a single Thread class:
Example