parallax 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 265d1dfafb3c018cd20546237fdfc939825fe54219853c0b72eaa6d73b7ec909
4
+ data.tar.gz: 5805b869b639d6b48a115056a532c1f72841611502ded3d2a99385edcea6347f
5
+ SHA512:
6
+ metadata.gz: e39236287fe4004b5d28026d63d925093a98dda0679ea0c6fb4708fd41d26e102ee7f01ee00ee76c17b6a25b44c8034e86efecfb9d72e9edc0a55d40e03c1fce
7
+ data.tar.gz: 01eb6f9e6d7cd0ac47783aae793144687f39daed896ce4d81f88306723b6ebec89c78330fc5c081ab0e96c5aa413f480a29c174960dc5a653d2e8edbd125fb10
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.3
7
+ before_install: gem install bundler -v 1.17.0
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in parallax.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,52 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ parallax (0.1.0)
5
+ activesupport
6
+ msgpack
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activesupport (5.2.1.1)
12
+ concurrent-ruby (~> 1.0, >= 1.0.2)
13
+ i18n (>= 0.7, < 2)
14
+ minitest (~> 5.1)
15
+ tzinfo (~> 1.1)
16
+ concurrent-ruby (1.1.3)
17
+ diff-lcs (1.3)
18
+ i18n (1.1.1)
19
+ concurrent-ruby (~> 1.0)
20
+ minitest (5.11.3)
21
+ msgpack (1.2.4)
22
+ rainbow (3.0.0)
23
+ rake (10.5.0)
24
+ rspec (3.8.0)
25
+ rspec-core (~> 3.8.0)
26
+ rspec-expectations (~> 3.8.0)
27
+ rspec-mocks (~> 3.8.0)
28
+ rspec-core (3.8.0)
29
+ rspec-support (~> 3.8.0)
30
+ rspec-expectations (3.8.2)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.8.0)
33
+ rspec-mocks (3.8.0)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.8.0)
36
+ rspec-support (3.8.0)
37
+ thread_safe (0.3.6)
38
+ tzinfo (1.2.5)
39
+ thread_safe (~> 0.1)
40
+
41
+ PLATFORMS
42
+ ruby
43
+
44
+ DEPENDENCIES
45
+ bundler (~> 1.17)
46
+ parallax!
47
+ rainbow
48
+ rake (~> 10.0)
49
+ rspec (~> 3.0)
50
+
51
+ BUNDLED WITH
52
+ 1.17.0
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Parallax
2
+
3
+ Parallax gem is a quick and simple framework to Ruby IPC and multi-core parallel execution.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'parallax'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install parallax
20
+
21
+ ## Usage
22
+
23
+ You can use this gem in many ways.
24
+
25
+ To execute parallel code with an array of elements, divide the elements in N groups and execute each chunk in one forked process.
26
+
27
+ ```ruby
28
+ array = [ 'running', 'code', 'in', 'parallel' ]
29
+
30
+ Parallax.execute array do |worker, array_chunk|
31
+ array_chunk.each do |element|
32
+ worker.send :log, "[#{worker.index}] #{element}"
33
+ end
34
+ end
35
+
36
+ # Example output with 4 cores:
37
+ # [1] code
38
+ # [3] parallel
39
+ # [0] running
40
+ # [2] in
41
+ ```
42
+
43
+ If you need inter-process communication, this can be done by calling `worker.send` and passing a list of arguments. The args are serialized and passed via IO pipe to a `collector` object, which is by default an instance of `Parallax::Collector` class. The collector then parses the args and treats them like a method call where the first arg is the name of the method. In the example above, the collectors calls the `log` method which prints the second arg. Of course you can specify your own collector, as long as it responds to a `collect(*args)` method.
44
+
45
+ ```ruby
46
+ class MyCollector
47
+ def receive(*args)
48
+ puts "Custom collector is receiving: #{args.inspect}"
49
+ end
50
+ end
51
+
52
+ array = [ 'running', 'code', 'in', 'parallel' ]
53
+
54
+ Parallax.execute array, collector: MyCollector do |worker, array_chunk|
55
+ array_chunk.each do |element|
56
+ worker.send "[#{worker.index}] #{element}"
57
+ end
58
+ end
59
+
60
+ # Example output with 4 cores:
61
+ # Custom collector is receiving: ["[1] code"]
62
+ # Custom collector is receiving: ["[3] parallel"]
63
+ # Custom collector is receiving: ["[0] running"]
64
+ # Custom collector is receiving: ["[2] in"]
65
+ ```
66
+
67
+ The collector object is returned by the `Parallax.execute` method, so if you need to store each worker processed data you can use the `worker.store` method or implement it in your own custom collector.
68
+
69
+ Other options you can pass to execute are:
70
+ * `processes`: the number of processes in which parallelize the execution. Defaults to `Etc.nprocessors` (which is equal to the number of cores of the current running machine).
71
+
72
+ Methods available for a `worker` object, which are collected in the `collector` object:
73
+ * `log(message)`: prints a message in stdout.
74
+ * `store(object)`: stores an object with timestamp and worker index.
75
+ * `rescue(error)`: rescues an error from a worker and raises the same error in the collector.
76
+ * `close`: closes worker communication with the collector.
77
+
78
+ ## Development
79
+
80
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
81
+
82
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
83
+
84
+ ## Contributing
85
+
86
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Pluvie/parallax.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "parallax"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,48 @@
1
+ module Parallax
2
+ class Collector
3
+
4
+ attr_accessor :workers_count
5
+ attr_accessor :workers_data
6
+ attr_accessor :closed_workers_count
7
+ attr_accessor :receiving_stream
8
+ attr_accessor :sending_stream
9
+
10
+ def initialize(workers_count)
11
+ @workers_count = workers_count
12
+ @closed_workers_count = 0
13
+ @receiving_stream, @sending_stream = IO.pipe
14
+ @workers_data = []
15
+ end
16
+
17
+ def collect
18
+ worker_index, method, *arguments = eval(@receiving_stream.gets.chomp)
19
+ self.send method, worker_index, *arguments
20
+ end
21
+
22
+ def log(worker_index, message)
23
+ puts message
24
+ end
25
+
26
+ def store(worker_index, object)
27
+ workers_data.push [ Time.now, worker_index, object ]
28
+ end
29
+
30
+ def rescue(worker_index, error_class, error_message)
31
+ raise error_class, "Worker #{worker_index} Error: #{error_message}"
32
+ end
33
+
34
+ def close_worker(worker_index)
35
+ @closed_workers_count += 1
36
+ end
37
+
38
+ def all_workers_terminated?
39
+ @closed_workers_count >= @workers_count
40
+ end
41
+
42
+ def close
43
+ @receiving_stream.close
44
+ @sending_stream.close
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module Parallax
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,37 @@
1
+ module Parallax
2
+ class Worker
3
+
4
+ attr_accessor :receiver
5
+ attr_accessor :index
6
+
7
+ def initialize(receiver, index)
8
+ @receiver = receiver
9
+ @index = index
10
+ end
11
+
12
+ def pack(*args)
13
+ [ self.index, *args ].inspect
14
+ end
15
+
16
+ def send(*args)
17
+ @receiver.sending_stream.puts pack(*args)
18
+ end
19
+
20
+ def log(message)
21
+ @receiver.sending_stream.puts pack(:log, message)
22
+ end
23
+
24
+ def store(object)
25
+ @receiver.sending_stream.puts pack(:store, object)
26
+ end
27
+
28
+ def rescue(error)
29
+ @receiver.sending_stream.puts pack(:rescue, error.class, error.message)
30
+ end
31
+
32
+ def close
33
+ @receiver.sending_stream.puts pack(:close_worker)
34
+ end
35
+
36
+ end
37
+ end
data/lib/parallax.rb ADDED
@@ -0,0 +1,46 @@
1
+ require 'active_support/core_ext/array'
2
+
3
+ require 'parallax/version'
4
+ require 'parallax/collector'
5
+ require 'parallax/worker'
6
+
7
+ module Parallax
8
+
9
+ class << self
10
+
11
+ ##
12
+ # Divides the given elements in groups of N and executes
13
+ # each chunk in parallel with the given block.
14
+ #
15
+ # @param [Array] elements processing elements.
16
+ # @param [Hash] options secondary options.
17
+ #
18
+ # @return [Collector] all processes output collector.
19
+ def execute(elements, options = {}, &block)
20
+ processes = options[:processes] || Etc.nprocessors
21
+
22
+ collector = Parallax::Collector.new(processes)
23
+ elements_chunks = elements.in_groups(processes, false)
24
+ processes.times do |worker_index|
25
+ Process.fork do
26
+ begin
27
+ worker = Parallax::Worker.new(collector, worker_index)
28
+ yield worker, elements_chunks[worker_index]
29
+ rescue StandardError => error
30
+ worker.rescue error
31
+ ensure
32
+ worker.close
33
+ end
34
+ end
35
+ end
36
+
37
+ until collector.all_workers_terminated?
38
+ collector.collect
39
+ end
40
+ collector.close
41
+ collector
42
+ end
43
+
44
+ end
45
+
46
+ end
data/parallax.gemspec ADDED
@@ -0,0 +1,32 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "parallax/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "parallax"
8
+ spec.version = Parallax::VERSION
9
+ spec.authors = ["Francesco Ballardin"]
10
+ spec.email = ["francesco.ballardin@develonproject.com"]
11
+
12
+ spec.summary = %q{Enhances Ruby inter-process communication, to boost your parallel code execution.}
13
+ spec.description = %q{Enhances Ruby inter-process communication, to boost your parallel code execution.}
14
+ spec.homepage = "https://github.com/Pluvie/ruby-parallax"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_dependency "msgpack"
26
+ spec.add_dependency "activesupport"
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.17"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ spec.add_development_dependency "rainbow"
32
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parallax
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Francesco Ballardin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: msgpack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.17'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.17'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rainbow
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Enhances Ruby inter-process communication, to boost your parallel code
98
+ execution.
99
+ email:
100
+ - francesco.ballardin@develonproject.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - lib/parallax.rb
115
+ - lib/parallax/collector.rb
116
+ - lib/parallax/version.rb
117
+ - lib/parallax/worker.rb
118
+ - parallax.gemspec
119
+ homepage: https://github.com/Pluvie/ruby-parallax
120
+ licenses: []
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.7.6
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Enhances Ruby inter-process communication, to boost your parallel code execution.
142
+ test_files: []