You could build your own pool but i wouldn t recommend it.
Ruby thread pool.
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.
These executors are lazy loaded so they do not create overhead when not needed.
Higher level abstractions use global thread pools by default for running asynchronous operations without creating new threads more often than necessary.
A future is somewhat a promise except you pass it a block to execute in another thread.
Thread pool a ruby antihero understanding a fundamental ruby abstraction for concurrency one of the fundamental concepts in key ruby libraries that embrace concurrency is the thread pool.
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.
A new thread will be created to execute the code in the block and the original thread will return from thread new immediately and resume execution with the next.
Thread pools allow you to control the number of active threads at any given time.
In this case it might be more beneficial to start a predefined set of.
Spawning one thread for each task is potentially expensive as threads are not free.
One solution is to use a thread pool.
Fortunately there is a better way.
The value returned by the block will be the value of the promise.
Concurrent ruby provides several global thread pools.
By default thread future executes the block in a newly created thread.
A simple executor style threadpool for ruby with tests yay usage.
Ruby threads are a lightweight and efficient way to achieve concurrency in your code.
Thread future accepts an optional argument of type thread pool if you want the block executed in an existing thread pool.
In the following example we are using the celluloid gem to do this for you.
To start a new thread just associate a block with a call to thread new.
Celluloid is now unmaintaned but the general idea of worker pools still applies.
You can find examples of thread pool implementations in gems like puma concurrent ruby celluloid pmap parallel and ruby thread.