codegrinder 0.0.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 +7 -0
- data/Gemfile.lock +52 -0
- data/README.md +71 -0
- data/codegrinder.gemspec +27 -0
- data/lib/codegrinder.rb +10 -0
- data/lib/codegrinder/processor.rb +70 -0
- data/lib/codegrinder/version.rb +14 -0
- data/lib/codegrinder/worker_thread_set.rb +32 -0
- data/samples/minipry.rb +29 -0
- data/spec/spec_helper.rb +96 -0
- metadata +54 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fc3ea67250438aecd1e7e009932be8c1c1f436fb
|
4
|
+
data.tar.gz: 02ac2c07c98258132216974f8e9f2de52de79b2c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3440d4e4cf1bfcc379f7cd3c957110e394361ac967b0ebc8c51217169185dc99c003926434f958ad0d7162ece255f000da791a39972edcf0eada44d2d58a7678
|
7
|
+
data.tar.gz: dc03d0f32bc722a67f94f34249d591a2d14383eab9735af484aaa9ec507ea6f07f98efa337f13702fa6ef031c283d9a1c3f9323759798973b66f96c43464616b
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
GEM
|
2
|
+
remote: https://rubygems.org/
|
3
|
+
specs:
|
4
|
+
ast (2.0.0)
|
5
|
+
astrolabe (1.3.0)
|
6
|
+
parser (>= 2.2.0.pre.3, < 3.0)
|
7
|
+
byebug (4.0.5)
|
8
|
+
columnize (= 0.9.0)
|
9
|
+
coderay (1.1.0)
|
10
|
+
columnize (0.9.0)
|
11
|
+
diff-lcs (1.2.5)
|
12
|
+
method_source (0.8.2)
|
13
|
+
parser (2.3.0.pre.2)
|
14
|
+
ast (>= 1.1, < 3.0)
|
15
|
+
powerpack (0.1.1)
|
16
|
+
pry (0.10.1)
|
17
|
+
coderay (~> 1.1.0)
|
18
|
+
method_source (~> 0.8.1)
|
19
|
+
slop (~> 3.4)
|
20
|
+
pry-byebug (3.1.0)
|
21
|
+
byebug (~> 4.0)
|
22
|
+
pry (~> 0.10)
|
23
|
+
rainbow (2.0.0)
|
24
|
+
rspec (3.3.0)
|
25
|
+
rspec-core (~> 3.3.0)
|
26
|
+
rspec-expectations (~> 3.3.0)
|
27
|
+
rspec-mocks (~> 3.3.0)
|
28
|
+
rspec-core (3.3.2)
|
29
|
+
rspec-support (~> 3.3.0)
|
30
|
+
rspec-expectations (3.3.1)
|
31
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
32
|
+
rspec-support (~> 3.3.0)
|
33
|
+
rspec-mocks (3.3.2)
|
34
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
35
|
+
rspec-support (~> 3.3.0)
|
36
|
+
rspec-support (3.3.0)
|
37
|
+
rubocop (0.32.1)
|
38
|
+
astrolabe (~> 1.3)
|
39
|
+
parser (>= 2.2.2.5, < 3.0)
|
40
|
+
powerpack (~> 0.1)
|
41
|
+
rainbow (>= 1.99.1, < 3.0)
|
42
|
+
ruby-progressbar (~> 1.4)
|
43
|
+
ruby-progressbar (1.7.5)
|
44
|
+
slop (3.6.0)
|
45
|
+
|
46
|
+
PLATFORMS
|
47
|
+
ruby
|
48
|
+
|
49
|
+
DEPENDENCIES
|
50
|
+
pry-byebug
|
51
|
+
rspec (~> 3.3)
|
52
|
+
rubocop
|
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# Will it grind ?
|
2
|
+
|
3
|
+
Yes, and in parallel !
|
4
|
+
|
5
|
+
codegrinder is a queue associated to one or several thread that share
|
6
|
+
the same logic, and process the elements inserted into it.
|
7
|
+
|
8
|
+
## How to install ?
|
9
|
+
|
10
|
+
`gem install codegrinder`
|
11
|
+
|
12
|
+
## How to use ?
|
13
|
+
|
14
|
+
First, you have to require codegrinder:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
require 'codegrinder'
|
18
|
+
```
|
19
|
+
|
20
|
+
Next, you have to build your codegrinder:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
# That codegrinder print the square of any inserted value:
|
24
|
+
my_grinder = CodeGrinder.new { |i| print(String(i ** 2) + "\n") }
|
25
|
+
```
|
26
|
+
|
27
|
+
At last, you can start it:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
# Starting the grinder with 4 threads. If no number is given, a single
|
31
|
+
# thread is used.
|
32
|
+
my_grinder.start(4)
|
33
|
+
```
|
34
|
+
|
35
|
+
From now on, any time you will insert a value in your grinder, it will be
|
36
|
+
squared and printed to screen by one of your 4 threads.
|
37
|
+
|
38
|
+
**Full example:**
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
# Parallelized tiny pry-like implementation
|
42
|
+
|
43
|
+
STDOUT.sync = true
|
44
|
+
STDERR.sync = true
|
45
|
+
|
46
|
+
require 'codegrinder'
|
47
|
+
|
48
|
+
code_processor = CodeGrinder.new do |code|
|
49
|
+
begin
|
50
|
+
STDERR.write " => #{eval(code).inspect}\n"
|
51
|
+
rescue StandardError => e
|
52
|
+
STDERR.write " => #{e.class}: #{e.message}\n"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
code_processor.start(4)
|
57
|
+
|
58
|
+
QUIT = 'quit'
|
59
|
+
|
60
|
+
def self.next_command
|
61
|
+
STDOUT.write("Enter command:\n")
|
62
|
+
result = gets
|
63
|
+
result.nil? ? QUIT : result.chomp
|
64
|
+
end
|
65
|
+
|
66
|
+
while (command = next_command) != QUIT
|
67
|
+
code_processor << command
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
This small ruby shell can process up to 4 commands at the same time.
|
data/codegrinder.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
|
4
|
+
require 'codegrinder/version'
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'codegrinder'
|
7
|
+
s.version = CodeGrinder::Version::STRING
|
8
|
+
s.summary = 'Multithreaded processing queue'
|
9
|
+
s.description = 'Allow to easily create a queue whose elements are processed in parallel threads'
|
10
|
+
s.authors = ['Alexandre Ignjatovic']
|
11
|
+
s.email = 'alexandre.ignjatovic@gmail.com'
|
12
|
+
s.files = `git ls-files`.split($RS).reject do |file|
|
13
|
+
file =~ %r{^(?:
|
14
|
+
spec/.*
|
15
|
+
samples/.*
|
16
|
+
|.*\.swp
|
17
|
+
|Gemfile
|
18
|
+
|Rakefile
|
19
|
+
|\.rspec
|
20
|
+
|\.gitignore
|
21
|
+
|\.rubocop.yml
|
22
|
+
)$}x
|
23
|
+
end
|
24
|
+
s.require_paths = ['lib']
|
25
|
+
s.homepage = 'https://github.com/bankair/codegrinder'
|
26
|
+
s.license = 'MIT'
|
27
|
+
end
|
data/lib/codegrinder.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'codegrinder/worker_thread_set'
|
4
|
+
require 'thread'
|
5
|
+
|
6
|
+
module CodeGrinder
|
7
|
+
##
|
8
|
+
# A Processor object is the association of a queue, and one or several
|
9
|
+
# threads that pop what is inserted into the queue, and process it
|
10
|
+
# according to the block passed at initialization time.
|
11
|
+
#
|
12
|
+
# You want a queue processed by several threads ? CodeGrinder::Processor is
|
13
|
+
# the guy.
|
14
|
+
class Processor < Queue
|
15
|
+
protected
|
16
|
+
|
17
|
+
attr_accessor :block, :threads, :running
|
18
|
+
|
19
|
+
public
|
20
|
+
|
21
|
+
##
|
22
|
+
# Initialize a new CodeGrinder::Processor.
|
23
|
+
# **Parameters:**
|
24
|
+
# * block: a proc applied to all elements inserted into the grinder.
|
25
|
+
def initialize(&block)
|
26
|
+
super()
|
27
|
+
self.block = block || fail(RuntimError, 'Missing block')
|
28
|
+
self.threads = WorkerThreadSet.null_object
|
29
|
+
self.running = false
|
30
|
+
end
|
31
|
+
|
32
|
+
def running?
|
33
|
+
running
|
34
|
+
end
|
35
|
+
|
36
|
+
DEFAULT_WORKER_COUNT = 1
|
37
|
+
|
38
|
+
##
|
39
|
+
# Start the code grinder processor.
|
40
|
+
# **Parameters:**
|
41
|
+
# * ng_workers: the number of parallel threads to use for that grinder.
|
42
|
+
# **Return:** self.
|
43
|
+
def start(nb_workers = DEFAULT_WORKER_COUNT)
|
44
|
+
fail('Cannot start a running thread') if running?
|
45
|
+
self.threads = WorkerThreadSet.new(nb_workers) do
|
46
|
+
# using self as a end of job marker
|
47
|
+
# (inserted in the stop method)
|
48
|
+
while ((args = pop) != self)
|
49
|
+
block.call(*args)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
self.running = true
|
53
|
+
self
|
54
|
+
end
|
55
|
+
|
56
|
+
##
|
57
|
+
# Stop the code grinder processor.
|
58
|
+
# **Return:** self.
|
59
|
+
def stop
|
60
|
+
return self unless running?
|
61
|
+
# using self as a end of job marker
|
62
|
+
# (tested in the start method)
|
63
|
+
threads.count.times { self << self }
|
64
|
+
threads.join
|
65
|
+
self.threads = WorkerThreadSet.null_object
|
66
|
+
self.running = false
|
67
|
+
self
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'thread'
|
4
|
+
require 'set'
|
5
|
+
|
6
|
+
##
|
7
|
+
# A set of worker threads, whose behavior is the very same
|
8
|
+
class WorkerThreadSet < Set
|
9
|
+
def initialize(count, &block)
|
10
|
+
fail(RuntimError, 'Missing block') if block.nil?
|
11
|
+
super()
|
12
|
+
count.times { self << Thread.new(&block) }
|
13
|
+
end
|
14
|
+
|
15
|
+
def join
|
16
|
+
each(&:join)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.null_object
|
20
|
+
NullWorkerThreadSet.instance
|
21
|
+
end
|
22
|
+
|
23
|
+
##
|
24
|
+
# Null object. Used as inert value
|
25
|
+
class NullWorkerThreadSet
|
26
|
+
require 'singleton'
|
27
|
+
include Singleton
|
28
|
+
def join
|
29
|
+
# Silently do nothing. Like some that guy I met at work, once.
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/samples/minipry.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Parallelized tiny pry-like implementation
|
2
|
+
|
3
|
+
STDOUT.sync = true
|
4
|
+
STDERR.sync = true
|
5
|
+
|
6
|
+
require 'codegrinder'
|
7
|
+
|
8
|
+
code_processor = CodeGrinder.new do |code|
|
9
|
+
begin
|
10
|
+
STDERR.write " => #{eval(code).inspect}\n"
|
11
|
+
rescue StandardError => e
|
12
|
+
STDERR.write " => #{e.class}: #{e.message}\n"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
code_processor.start(4)
|
17
|
+
|
18
|
+
QUIT = 'quit'
|
19
|
+
|
20
|
+
def self.next_command
|
21
|
+
STDOUT.write("Enter command:\n")
|
22
|
+
result = gets
|
23
|
+
result.nil? ? QUIT : result.chomp
|
24
|
+
end
|
25
|
+
|
26
|
+
while (command = next_command) != QUIT
|
27
|
+
code_processor << command
|
28
|
+
end
|
29
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
4
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
5
|
+
# files.
|
6
|
+
#
|
7
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
8
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
9
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
10
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
11
|
+
# a separate helper file that requires the additional dependencies and performs
|
12
|
+
# the additional setup, and require it from the spec files that actually need
|
13
|
+
# it.
|
14
|
+
#
|
15
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
16
|
+
# users commonly want.
|
17
|
+
#
|
18
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
19
|
+
RSpec.configure do |config|
|
20
|
+
# rspec-expectations config goes here. You can use an alternate
|
21
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
22
|
+
# assertions if you prefer.
|
23
|
+
config.expect_with :rspec do |expectations|
|
24
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
25
|
+
# and `failure_message` of custom matchers include text for helper methods
|
26
|
+
# defined using `chain`, e.g.:
|
27
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
28
|
+
# # => "be bigger than 2 and smaller than 4"
|
29
|
+
# ...rather than:
|
30
|
+
# # => "be bigger than 2"
|
31
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
32
|
+
end
|
33
|
+
|
34
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
35
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
36
|
+
config.mock_with :rspec do |mocks|
|
37
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
38
|
+
# a real object. This is generally recommended, and will default to
|
39
|
+
# `true` in RSpec 4.
|
40
|
+
mocks.verify_partial_doubles = true
|
41
|
+
end
|
42
|
+
|
43
|
+
# The settings below are suggested to provide a good initial experience
|
44
|
+
# with RSpec, but feel free to customize to your heart's content.
|
45
|
+
=begin
|
46
|
+
# These two settings work together to allow you to limit a spec run
|
47
|
+
# to individual examples or groups you care about by tagging them with
|
48
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
49
|
+
# get run.
|
50
|
+
config.filter_run :focus
|
51
|
+
config.run_all_when_everything_filtered = true
|
52
|
+
|
53
|
+
# Allows RSpec to persist some state between runs in order to support
|
54
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
55
|
+
# you configure your source control system to ignore this file.
|
56
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
57
|
+
|
58
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
59
|
+
# recommended. For more details, see:
|
60
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
61
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
62
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
63
|
+
config.disable_monkey_patching!
|
64
|
+
|
65
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
66
|
+
# be too noisy due to issues in dependencies.
|
67
|
+
config.warnings = true
|
68
|
+
|
69
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
70
|
+
# file, and it's useful to allow more verbose output when running an
|
71
|
+
# individual spec file.
|
72
|
+
if config.files_to_run.one?
|
73
|
+
# Use the documentation formatter for detailed output,
|
74
|
+
# unless a formatter has already been configured
|
75
|
+
# (e.g. via a command-line flag).
|
76
|
+
config.default_formatter = 'doc'
|
77
|
+
end
|
78
|
+
|
79
|
+
# Print the 10 slowest examples and example groups at the
|
80
|
+
# end of the spec run, to help surface which specs are running
|
81
|
+
# particularly slow.
|
82
|
+
config.profile_examples = 10
|
83
|
+
|
84
|
+
# Run specs in random order to surface order dependencies. If you find an
|
85
|
+
# order dependency and want to debug it, you can fix the order by providing
|
86
|
+
# the seed, which is printed after each run.
|
87
|
+
# --seed 1234
|
88
|
+
config.order = :random
|
89
|
+
|
90
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
91
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
92
|
+
# test failures related to randomization by passing the same `--seed` value
|
93
|
+
# as the one that triggered the failure.
|
94
|
+
Kernel.srand config.seed
|
95
|
+
=end
|
96
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: codegrinder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexandre Ignjatovic
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-15 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Allow to easily create a queue whose elements are processed in parallel
|
14
|
+
threads
|
15
|
+
email: alexandre.ignjatovic@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- Gemfile.lock
|
21
|
+
- README.md
|
22
|
+
- codegrinder.gemspec
|
23
|
+
- lib/codegrinder.rb
|
24
|
+
- lib/codegrinder/processor.rb
|
25
|
+
- lib/codegrinder/version.rb
|
26
|
+
- lib/codegrinder/worker_thread_set.rb
|
27
|
+
- samples/minipry.rb
|
28
|
+
- spec/spec_helper.rb
|
29
|
+
homepage: https://github.com/bankair/codegrinder
|
30
|
+
licenses:
|
31
|
+
- MIT
|
32
|
+
metadata: {}
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 2.0.14
|
50
|
+
signing_key:
|
51
|
+
specification_version: 4
|
52
|
+
summary: Multithreaded processing queue
|
53
|
+
test_files: []
|
54
|
+
has_rdoc:
|