bun-threads
    Preparing search index...

    Class ThreadPool<T>

    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:

    import { Thread, ThreadPool } from "bun-threads";

    const thread = new Thread((wait: number) => Bun.sleepSync(wait)) // simulate some synchronous work
    const threadPool = new ThreadPool((wait: number) => Bun.sleepSync(wait)) // simulate some synchronous work

    let start = performance.now()
    await Promise.all([,
    thread.run([1_000]),
    thread.run([1_000]),
    thread.run([1_000])
    ])
    // a single Thread can only execute synchronous tasks one at a time
    console.log('Thread completed in:', performance.now() - start, 'ms') // ~ 3000 ms

    start = performance.now()
    await Promise.all([,
    threadPool.run([1_000]),
    threadPool.run([1_000]),
    threadPool.run([1_000])
    ])
    // ThreadPool runs each task in a separate Thread in parallel
    console.log('ThreadPool completed in:', performance.now() - start, 'ms') // ~ 1000 ms

    thread.close()
    threadPool.close()

    Type Parameters

    • T extends (...args: any[]) => any

      The signature of your callback function, including its arguments and return type.

    Index

    Constructors

    Properties

    Accessors

    Methods

    Constructors

    • Create a new ThreadPool to group worker threads together and run multiple worker threads in parallel.

      Type Parameters

      • T extends (...args: any[]) => any

      Parameters

      • fn: T

        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.

      • Optionaloptions: ThreadPoolOptions

      Returns ThreadPool<T>

    Properties

    fn: T

    The callback function to be executed in parallel upon calling the asychronous run method.

    Accessors

    • get busy(): number

      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.

      Returns number

      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()
    • get idle(): Promise<void>

      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 Threads are busy. Works similar to the Thread.idle property, except that the promise resolves to void.

      Returns Promise<void>

    • get idleTimeout(): number

      How long (in milliseconds) to keep the dynamic Threads in the ThreadPool active after completing a task before terminating them. minThreads number of Threads 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.

      Returns number

      0
      

      RangeError if value < 0

    • set idleTimeout(value: number): void

      How long (in milliseconds) to keep the dynamic Threads in the ThreadPool active after completing a task before terminating them. minThreads number of Threads 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.

      Parameters

      • value: number

      Returns void

      0
      

      RangeError if value < 0

    • get maxThreads(): number

      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.

      Returns number

      os.availableParallelism() - 1
      

      RangeError if value < 1 or if value is not an integer

    • set maxThreads(value: number): void

      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.

      Parameters

      • value: number

      Returns void

      os.availableParallelism() - 1
      

      RangeError if value < 1 or if value is not an integer

    • get minThreads(): number

      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 Threads 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.

      Returns number

      1
      

      RangeError if value < 0 or if value is not an integer

    • set minThreads(value: number): void

      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 Threads 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.

      Parameters

      • value: number

      Returns void

      1
      

      RangeError if value < 0 or if value is not an integer

    Methods

    • 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.

      Parameters

      • Optionalforce: boolean = false

        This method will wait for the Thread to finish its queued tasks unless force is set to true. Default is false.

      Returns Promise<number>

      a Promise<number> that resolves to the amount of open threads that were actually closed (how many Thread.close() calls returned true).

      import { ThreadPool } from "bun-threads";

      const threadPool = new ThreadPool(() => { return 42 })
      console.log('The answer is:', await threadPool.run([]))
      threadPool.close() // not calling close may cause the program to hang
    • Execute the callback that was specified in the constructor in a separate worker thread.

      Parameters

      • args: Parameters<T>

        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.

      Returns Promise<ReturnType<T>>

      A Promise<ReturnType<T>> that resolves to the return type of your callback function.