title: Limit the number of threads in an iteration ruby
body: <p>When I have my code like this, I get &quot;can't create thread, resource temporarily unavailable&quot;.  There are over 24k files in the directory to process.</p>
  <pre><code>frames.each do |image|
      Thread.new do
          pipeline = ImageProcessing::MiniMagick.
              source(File.open(&quot;original/#{image}&quot;))
              .append(&quot;-fuzz&quot;, &quot;30%&quot;)
              .append(&quot;-transparent&quot;, &quot;#ff00fe&quot;)
          result = pipeline.call
  
          puts result.path
          file_parts = image.split(&quot;_&quot;)
          frame_number = file_parts[2]
          FileUtils.cp(result.path, &quot;transparent/image_transparent_#{frame_number}&quot;)
  
          puts &quot;Done with #{image}!&quot;
          puts &quot;#{Dir.children(&quot;transparent&quot;).count.to_s} / #{Dir.children(&quot;original&quot;).count.to_s}&quot;
          puts &quot;\n&quot;   
      end
  end.each{ |thread| thread.join }
  </code></pre>
  <p>So, I tried the first 1001 files by calling the index 0-1000, and did it this way:</p>
  <pre><code>frames[0..1000].each_with_index do |image, index|
      thread = Thread.new do
          pipeline = ImageProcessing::MiniMagick.
              source(File.open(&quot;original/#{image}&quot;))
              .append(&quot;-fuzz&quot;, &quot;30%&quot;)
              .append(&quot;-transparent&quot;, &quot;#ff00fe&quot;)
          result = pipeline.call
  
          puts result.path
          file_parts = image.split(&quot;_&quot;)
          frame_number = file_parts[2]
          FileUtils.cp(result.path, &quot;transparent/image_transparent_#{frame_number}&quot;)
  
          puts &quot;Done with #{image}!&quot;
          puts &quot;#{Dir.children(&quot;transparent&quot;).count.to_s} / #{Dir.children(&quot;original&quot;).count.to_s}&quot;
          puts &quot;\n&quot;   
      end
      thread.join
  end
  </code></pre>
  <p>And while this is processing, the speed seems to be about the same as if it was on a single thread when I'm watching it in the Terminal.</p>
  <p>But I want the code to be able to limit to whatever the OS will allow before it disallows, so that it can parse through them all faster.</p>
  <p>Or at lease:</p>
  <ol>
  <li>Find the maximum threads allowed</li>
  <li>Get original directory's count, divided by the number of threads allowed.</li>
  <li>Run this each in batches of that division.</li>
  </ol>