Project

pthread

0.0
No commit activity in last 3 years
No release in over 3 years
Parallel threads in Ruby using dRb
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.3
>= 0
>= 0
 Project Readme

Pthread

Module Pthread provides possibility for Ruby to run pieces of code in parallel using additional Unix processes on the same or other machines in the network.

Build Status

Installation

Add this line to your application's Gemfile:

gem 'pthread'

And then execute:

$ bundle

Or install it yourself as:

$ gem install pthread

Usage

Before using parallel threads you should configure a dRb server:

Pthread::Pthread.start_service '192.168.1.100:12345'

Pthreads are actual Unix processes. You can add process-workers on the same machine as the main programm by calling:

Pthread::Pthread.add_executor 'tasks'

or

Pthread::Pthread.add_executors 5, 'tasks'

Methods #add_executor and #add_executors take an optional parameter that specifies a queue name.

In order to connect an executor from a separate machine in your programm you can call:

Pthread::PthreadExecutor.new '192.168.1.100:12345', 'tasks'

specifing the host and a desired queue.

Now you can spawn Pthreads in order to gain multicore performance by providing name of the queue, code to be executed and context variables:

pthread = Pthread::Pthread.new queue: 'tasks', code: %{
  x ** 2
}, context: { x: 5 }

When you need to get the value back simple call

pthread.value # => 25

Exceptions

If exception is raised inside a pthread in doesn't affect the whole process. Exception is raise by accesing pthread's value.

pthread.value # => raise exception

Stop executors

To stop executors on the server machine simply run

Pthread::Pthread.kill_executors

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request