threadz icon indicating copy to clipboard operation
threadz copied to clipboard

A Ruby threadpool library to handle threadpools and make batch jobs easier

= Threadz Thread Pool Library

{Build Status}[https://travis-ci.org/nanodeath/threadz] {}[https://codeclimate.com/github/nanodeath/threadz] {Dependency Status}[https://gemnasium.com/nanodeath/threadz]

== Description

This is a thread pool library that you can do two main things with, which I'll demonstrate in code:

These are more for "fire and forget" tasks

T1 = Threadz::ThreadPool.new T1.process { puts "my first task" } T1.process { puts "my second task" }

If you care when the tasks complete, use batches

T2 = Threadz::ThreadPool.new b = T2.new_batch b << lambda { puts "my first task" } b << lambda { puts "my second task" }

puts "do a couple of other things..."

b.wait_until_done

You can do other things, too

T3 = Threadz::ThreadPool.new b = T3.new_batch b << lambda { puts "my first task" } b << lambda { puts "my second task" }

puts "do a couple of other things..."

b.when_done { puts "woohoo, done with tasks" }

puts "and some other stuff, blah"

b = T3.new_batch b << lambda { 10000000.times {} }

b.wait_until_done(:timeout => 0.1) puts b.completed? ? "finished!" : "didn't finish"

Error handling

b = T3.new_batch(:max_retries => 3) b << lambda { raise } b.wait_until_done puts b.errors

b = T3.new_batch(:max_retries => 3, :error_handler => lambda { |error, control| puts "Error! #{error}" }) b << lambda { raise } b.wait_until_done

See the specs for more error handling stuff. Much better examples.

The thread pool is also smart -- depending on load, it can either spawn or cull additional threads (at a rate you can set).

== Examples

For examples, please see the well-documented specs. They're all fairly simple and straightforward. Please message me if you have issues that aren't answered by reading the spec.