pthread 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pthread.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Nikita Cernovs
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # Pthread
2
+
3
+ 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.
4
+
5
+ [![Build Status](https://travis-ci.org/nikitachernov/Pthread.png)](https://travis-ci.org/nikitachernov/Pthread)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'pthread'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install pthread
20
+
21
+ ## Usage
22
+
23
+ Before using parallel threads you should configure a dRb server:
24
+
25
+ Pthread::Pthread.start_service 'localhost:12345'
26
+
27
+ Pthreads are actual Unix processes. You can add process-workers on the same machine as the main programm by calling:
28
+
29
+ Pthread::Pthread.add_executor 'tasks'
30
+
31
+ or
32
+
33
+ Pthread::Pthread.add_executors 5, 'tasks'
34
+
35
+ Methods #add_executor and #add_executors take an optional parameter that specifies a queue name.
36
+
37
+ In order to connect an executor from a separate machine in your programm you can call
38
+
39
+ PthreadExecutor.new '192.168.1.100:12345', 'tasks'
40
+
41
+ specifing the host and a desired queue.
42
+
43
+ Now you can spawn Pthreads in gain multicore performance by providing name of the queue, code to be executed and context variables:
44
+
45
+ Pthread::Pthread.new queue: 'tasks', code: %{
46
+ x ** 2
47
+ }, context: { x: 5 }
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ task :default => [:test]
4
+
5
+ task :test do
6
+ sh 'rspec spec'
7
+ end
@@ -0,0 +1,31 @@
1
+ require 'rinda/tuplespace'
2
+
3
+ class Pthread::Pthread
4
+ @@ts = Rinda::TupleSpace.new
5
+
6
+ def self.start_service(host)
7
+ @@host = host
8
+ DRb.start_service("druby://#{@@host}", @@ts)
9
+ end
10
+
11
+ def self.add_executors(count = 1, queue=nil)
12
+ count.times do
13
+ fork do
14
+ DRb.stop_service
15
+ Pthread::PthreadExecutor.new(@@host, queue)
16
+ end
17
+ end
18
+ end
19
+
20
+ def self.add_executor(queue=nil)
21
+ add_executors(1, queue)
22
+ end
23
+
24
+ def initialize(job)
25
+ @@ts.write(["#{self.object_id}_s", job[:queue], job[:code], job[:context]])
26
+ end
27
+
28
+ def value
29
+ @@ts.take(["#{self.object_id}_r", nil])[1]
30
+ end
31
+ end
@@ -0,0 +1,21 @@
1
+ require 'drb/drb'
2
+
3
+ class Pthread::PthreadExecutor
4
+ def initialize(host, queue=nil)
5
+ DRb.start_service
6
+ ts = DRbObject.new_with_uri("druby://#{host}")
7
+
8
+ loop do
9
+ pthread_id, _, code, context = ts.take([ /_s/, queue, nil, nil])
10
+
11
+ context && context.each do |a, v|
12
+ singleton_class.class_eval { attr_accessor a }
13
+ self.send("#{a}=", context[a])
14
+ end
15
+
16
+ ts.write(["#{pthread_id[0..-3]}_r", eval(code)])
17
+ end
18
+ rescue DRb::DRbConnError
19
+ exit 0
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module Pthread
2
+ VERSION = '0.0.1'
3
+ end
data/lib/pthread.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'pthread/version'
2
+ require 'pthread/pthread_executor'
3
+ require 'pthread/pthread'
data/pthread.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pthread/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'pthread'
8
+ spec.version = Pthread::VERSION
9
+ spec.authors = ['Nikita Cernovs']
10
+ spec.email = ['nikita.cernovs@gmail.com']
11
+ spec.description = %q{Parallel threads in Ruby using dRb}
12
+ spec.summary = %q{Parallel threads in Ruby}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rspec'
23
+ spec.add_development_dependency 'rake'
24
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Pthread::Pthread do
4
+
5
+ let(:pthread) do
6
+ Pthread::Pthread.new queue: 'tasks', code: %{
7
+ x ** 2
8
+ }, context: { x: 5 }
9
+ end
10
+
11
+ before do
12
+ Pthread::Pthread.start_service 'localhost:12345'
13
+ Pthread::Pthread.add_executor 'tasks'
14
+ end
15
+
16
+ it 'should calculate value in a separate process' do
17
+ pthread.value.should eq 25
18
+ end
19
+ end
@@ -0,0 +1 @@
1
+ require 'pthread'
data/spec_helper.rb ADDED
@@ -0,0 +1 @@
1
+ require 'pthread'
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pthread
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nikita Cernovs
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Parallel threads in Ruby using dRb
63
+ email:
64
+ - nikita.cernovs@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .travis.yml
71
+ - Gemfile
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - lib/pthread.rb
76
+ - lib/pthread/pthread.rb
77
+ - lib/pthread/pthread_executor.rb
78
+ - lib/pthread/version.rb
79
+ - pthread.gemspec
80
+ - spec/pthread/pthread_spec.rb
81
+ - spec/spec_helper.rb
82
+ - spec_helper.rb
83
+ homepage: ''
84
+ licenses:
85
+ - MIT
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 1.8.25
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: Parallel threads in Ruby
108
+ test_files:
109
+ - spec/pthread/pthread_spec.rb
110
+ - spec/spec_helper.rb