workload 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 +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +69 -0
- data/Rakefile +1 -0
- data/lib/workload/version.rb +3 -0
- data/lib/workload.rb +83 -0
- data/workload.gemspec +23 -0
- metadata +81 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 68f84c27c7e35d59c365cf9a57a3311e2bda1201
|
4
|
+
data.tar.gz: 38bb93842bbc692abd4cbed1c7ba76f4221034d7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3272e29e17818a405b50bde1d3c73afdd55078e2065a6afb73973284ddd613992a6aaf3e11932d549fab00d2e55d4fa7687dd228f0ec67b87fd50f9c7e9d2429
|
7
|
+
data.tar.gz: 49a97815d287e89d0c9b5ccab5e53f8e87bc1f5e2751c2a74e52cd0788cae703d3f462694bd99292c302bab68b440f5f6041350511acfb727efb1209b07f0c8a
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Tymon Tobolski, Monterail.com, LLC
|
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,69 @@
|
|
1
|
+
# Workload
|
2
|
+
|
3
|
+
Distribute simple tasks to multiple threads with ease.
|
4
|
+
|
5
|
+
This is an dead simple implementation of multithreaded [producer-consumer](http://en.wikipedia.org/wiki/Producer%E2%80%93consumer_problem) pattern we at [Monterail](http://monterail.com/) are using quite often during imports/exports and all sort of data migration between systems. I'm just tired of writing all that again and again so that's why I put that into a 80LOC gem.
|
6
|
+
|
7
|
+
|
8
|
+

|
9
|
+
|
10
|
+
Each `producer` and `consumer` instance gets it's own thread.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
```bash
|
15
|
+
$ gem install workload
|
16
|
+
```
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
Let's say we have some big file that we need to read and then do something
|
21
|
+
with every line of that file and that processing can be done in parallel.
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
# job.rb
|
25
|
+
require "rubygems"
|
26
|
+
require "workload"
|
27
|
+
|
28
|
+
Workload.run do |p|
|
29
|
+
p.produce(1) do |queue| # 1 is the default number of producer threads
|
30
|
+
while line = STDIN.gets
|
31
|
+
queue << line # The pipe is simple ruby Queue
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
p.consume(5) do |line| # Here we are spawning 5 consumer threads
|
36
|
+
print "[#{Thread.current[:id]}] consuming line: #{line}"
|
37
|
+
# do something useful with that input
|
38
|
+
end
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
and then just run
|
43
|
+
|
44
|
+
```
|
45
|
+
$ cat huge-file.txt | ruby job.rb
|
46
|
+
```
|
47
|
+
|
48
|
+
## FAQ
|
49
|
+
|
50
|
+
- What about distributing using sidekiq (or any other queue)?
|
51
|
+
- It's too much for such simple task and usually requires some storage (e.g. redis)
|
52
|
+
|
53
|
+
- So why not celluloid?
|
54
|
+
- It's also too big and complicated
|
55
|
+
|
56
|
+
- What about thread safety?
|
57
|
+
- There is none. I mean, ruby's [Queue](http://ruby-doc.org/stdlib-2.0.0/libdoc/thread/rdoc/Queue.html) is thread safe and all that but if you use some global state inside `produce` or `consume` blocks than you gonna have a bad time.
|
58
|
+
|
59
|
+
- Error handling?
|
60
|
+
- If something bad happen inside `consume` block it will be recorded and all errors will be displayed at the end of execution
|
61
|
+
|
62
|
+
|
63
|
+
## Contributing
|
64
|
+
|
65
|
+
1. Fork it
|
66
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
67
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
68
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
69
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/workload.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
require "workload/version"
|
2
|
+
|
3
|
+
module Workload
|
4
|
+
class Gen
|
5
|
+
DEFAULT_OPTS = {
|
6
|
+
:sleep => 0.1
|
7
|
+
}.freeze
|
8
|
+
|
9
|
+
def initialize(opts = {}, &block)
|
10
|
+
@opts = DEFAULT_OPTS.merge(opts)
|
11
|
+
@queue = Queue.new
|
12
|
+
@errors = []
|
13
|
+
|
14
|
+
block.call(self)
|
15
|
+
end
|
16
|
+
|
17
|
+
def produce(n = 1, &block)
|
18
|
+
@producers_num = n
|
19
|
+
@produce = block
|
20
|
+
end
|
21
|
+
|
22
|
+
def consume(n = 1, &block)
|
23
|
+
@consumers_num = n
|
24
|
+
@consume = block
|
25
|
+
end
|
26
|
+
|
27
|
+
def make_producers
|
28
|
+
@producers_num.times.map do |i|
|
29
|
+
Thread.new do
|
30
|
+
@produce.call(@queue)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def make_consumers
|
36
|
+
@consumers_num.times.map do |i|
|
37
|
+
Thread.new(i) do |j|
|
38
|
+
Thread.current[:id] = j
|
39
|
+
while @keep || !@queue.empty?
|
40
|
+
begin
|
41
|
+
item = @queue.pop(true)
|
42
|
+
@consume.call(item)
|
43
|
+
rescue ThreadError => ex
|
44
|
+
sleep @opts[:sleep]
|
45
|
+
rescue => ex
|
46
|
+
print "ERROR: #{item} => #{ex}\n"
|
47
|
+
@errors << [item, ex]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def display_errors
|
55
|
+
unless @errors.empty?
|
56
|
+
puts "\nErrors:\n"
|
57
|
+
@errors.each do |item, ex|
|
58
|
+
puts "Item #{item} errored with: #{ex.message}"
|
59
|
+
puts ex.backtrace.map {|e| " #{e}"}.join("\n")
|
60
|
+
puts
|
61
|
+
end
|
62
|
+
|
63
|
+
exit 1
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def run!
|
68
|
+
@keep = true
|
69
|
+
@producers = make_producers
|
70
|
+
@consumers = make_consumers
|
71
|
+
|
72
|
+
@producers.each(&:join)
|
73
|
+
@keep = false
|
74
|
+
@consumers.each(&:join)
|
75
|
+
|
76
|
+
display_errors
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def self.run(opts = {}, &block)
|
81
|
+
Gen.new(opts, &block).run!
|
82
|
+
end
|
83
|
+
end
|
data/workload.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'workload/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "workload"
|
8
|
+
spec.version = Workload::VERSION
|
9
|
+
spec.authors = ["Tymon Tobolski"]
|
10
|
+
spec.email = ["tymon.tobolski@monterail.com"]
|
11
|
+
spec.description = %q{Distribute simple tasks to multiple threads with ease}
|
12
|
+
spec.summary = %q{Distribute simple tasks to multiple threads with ease}
|
13
|
+
spec.homepage = "http://github.com/monterail/workload"
|
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 "rake"
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: workload
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tymon Tobolski
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Distribute simple tasks to multiple threads with ease
|
42
|
+
email:
|
43
|
+
- tymon.tobolski@monterail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- lib/workload.rb
|
54
|
+
- lib/workload/version.rb
|
55
|
+
- workload.gemspec
|
56
|
+
homepage: http://github.com/monterail/workload
|
57
|
+
licenses:
|
58
|
+
- MIT
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.1.3
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: Distribute simple tasks to multiple threads with ease
|
80
|
+
test_files: []
|
81
|
+
has_rdoc:
|