You can find examples of thread pool implementations in gems like puma concurrent ruby celluloid pmap parallel and ruby thread.
Ruby thread pool.
Concurrent ruby provides several global thread pools.
Higher level abstractions use global thread pools by default for running asynchronous operations without creating new threads more often than necessary.
In this case it might be more beneficial to start a predefined set of.
Thread pools are particularly useful when there are a large number of short tasks to be performed rather than a small number of longer tasks.
Each thread should be wrapped before it runs application code so if your application manually delegates work to other threads such as via thread new or concurrent ruby features that use thread pools you should immediately wrap the block.
By default thread future executes the block in a newly created thread.
One solution is to use a thread pool.
To start a new thread just associate a block with a call to thread new.
Require rubygems require thread pool pool threadpool new threads 10 pool execute puts i m writing from a thread pool join it s often useful to make sure that the properties of ruby s blocks don t bite you.
The value returned by the block will be the value of the promise.
Ruby thread pool a thread pool is useful when you wish to do some work in a thread but do not know how much work you will be doing in advance.
Celluloid is now unmaintaned but the general idea of worker pools still applies.
A thread pool is a group of pre instantiated reusable threads that are available to perform work as needed.
Ruby threads are a lightweight and efficient way to achieve concurrency in your code.
Fortunately there is a better way.
Spawning one thread for each task is potentially expensive as threads are not free.
Thread future accepts an optional argument of type thread pool if you want the block executed in an existing thread pool.
Thread pools allow you to control the number of active threads at any given time.
A future is somewhat a promise except you pass it a block to execute in another thread.
You could build your own pool but i wouldn t recommend it.
In the following example we are using the celluloid gem to do this for you.
A simple executor style threadpool for ruby with tests yay usage.