silent_worker 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/.travis.yml +5 -0
- data/Gemfile +9 -0
- data/LICENSE.txt +22 -0
- data/README.md +79 -0
- data/Rakefile +1 -0
- data/lib/silent_worker/version.rb +3 -0
- data/lib/silent_worker.rb +56 -0
- data/silent_worker.gemspec +23 -0
- data/spec/silent_worker_spec.rb +55 -0
- data/spec/spec_helper.rb +20 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 75f1cc3440e337efc5b7398ecacd5d6c4eda4f83
|
4
|
+
data.tar.gz: 12d8622c2fff8e6ffb5cda906a1871d0dd987bb9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 128400770e82d7a575465e854abe0242469919fa6c376b8734537c7ac4481008cbbdfd86d0ef1c31dbbbc8ec3553bbb8c7284a6f6f6e80c0ab96a92e945b9e0c
|
7
|
+
data.tar.gz: 5d4fb823c01d0a18095cb9ff13a9e716ed3a7ab72b58fb1d37d5b55b97102ef67cc6026efa48fbade255f6fe7c930335275768ebcf178071dbc37fbf9e5e0b6f
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 uu59
|
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,79 @@
|
|
1
|
+
[](https://travis-ci.org/uu59/silent_worker)
|
2
|
+
[](https://coveralls.io/r/uu59/silent_worker?branch=master)
|
3
|
+
|
4
|
+
# SilentWorker
|
5
|
+
|
6
|
+
SilentWorker gives simple worker thread model.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'silent_worker'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install silent_worker
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
sw = SilentWorker.new(2) do |data|
|
26
|
+
puts data
|
27
|
+
sleep 0.01
|
28
|
+
end
|
29
|
+
|
30
|
+
10.times |n|
|
31
|
+
sw << n
|
32
|
+
end
|
33
|
+
|
34
|
+
sw.wait
|
35
|
+
|
36
|
+
# =>
|
37
|
+
# 1
|
38
|
+
# 0
|
39
|
+
# 2
|
40
|
+
# 3
|
41
|
+
# 4
|
42
|
+
# 5
|
43
|
+
# 6
|
44
|
+
# 7
|
45
|
+
# 8
|
46
|
+
# 9
|
47
|
+
|
48
|
+
# NOTE: Order is a random
|
49
|
+
```
|
50
|
+
|
51
|
+
Real world example:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
parallel = 8
|
55
|
+
|
56
|
+
sw = SilentWorker.new(parallel) do |url|
|
57
|
+
system("wget", url)
|
58
|
+
end
|
59
|
+
|
60
|
+
File.open('url_list.txt') do |f|
|
61
|
+
sw << f.gets.strip
|
62
|
+
end
|
63
|
+
|
64
|
+
File.open('url_list2.txt') do |f|
|
65
|
+
sw << f.gets.strip
|
66
|
+
end
|
67
|
+
|
68
|
+
sw.wait
|
69
|
+
```
|
70
|
+
|
71
|
+
This example will be 8 paralleled `wget` from `url_list.txt` and `url_list2.txt` such as `cat url_list.txt url_list2.txt | xargs -P8 -n1 wget`.
|
72
|
+
|
73
|
+
## Contributing
|
74
|
+
|
75
|
+
1. Fork it
|
76
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
77
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
78
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
79
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require "thread"
|
2
|
+
require "silent_worker/version"
|
3
|
+
|
4
|
+
class SilentWorker
|
5
|
+
attr_reader :job, :workers, :queue
|
6
|
+
|
7
|
+
FINISH_DATA = "\x04" # EOT
|
8
|
+
|
9
|
+
def initialize(workers = 32, &job)
|
10
|
+
@job = job
|
11
|
+
@workers = workers
|
12
|
+
@threads = []
|
13
|
+
@queue = Queue.new
|
14
|
+
start
|
15
|
+
end
|
16
|
+
|
17
|
+
def <<(data)
|
18
|
+
@queue.push(data)
|
19
|
+
end
|
20
|
+
|
21
|
+
def wait
|
22
|
+
finish!
|
23
|
+
@workers.times do
|
24
|
+
@queue.push(FINISH_DATA)
|
25
|
+
end
|
26
|
+
@threads.each(&:join)
|
27
|
+
end
|
28
|
+
|
29
|
+
def abort
|
30
|
+
@threads.each(&:kill)
|
31
|
+
wait
|
32
|
+
end
|
33
|
+
|
34
|
+
def stop
|
35
|
+
wait
|
36
|
+
end
|
37
|
+
|
38
|
+
def start
|
39
|
+
@workers.times do
|
40
|
+
@threads << Thread.start(@job, @queue) do |job, queue|
|
41
|
+
loop do
|
42
|
+
data = queue.pop
|
43
|
+
break if @finished && data == FINISH_DATA
|
44
|
+
job.call(data)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def finish!
|
53
|
+
@finished = true
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -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 'silent_worker/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "silent_worker"
|
8
|
+
spec.version = SilentWorker::VERSION
|
9
|
+
spec.authors = ["uu59"]
|
10
|
+
spec.email = ["k@uu59.org"]
|
11
|
+
spec.description = %q{SilentWorker gives simple worker thread model}
|
12
|
+
spec.summary = %q{SilentWorker gives simple worker thread model}
|
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
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# -- coding: utf-8
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe SilentWorker do
|
6
|
+
describe "#new" do
|
7
|
+
it "invoke #start" do
|
8
|
+
SilentWorker.any_instance.should_receive(:start)
|
9
|
+
SilentWorker.new {|data| data }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#<<" do
|
14
|
+
it "given arg pass to job" do
|
15
|
+
data = 42
|
16
|
+
job = proc {|given| given.should == data}
|
17
|
+
sw = SilentWorker.new(&job)
|
18
|
+
sw << data
|
19
|
+
sw.wait
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "#wait" do
|
24
|
+
it "worker threads exit after jobs completed" do
|
25
|
+
sw = SilentWorker.new {|data| data }
|
26
|
+
expect{ Thread.list.length > sw.workers }.to be_true
|
27
|
+
sw.wait
|
28
|
+
expect{ Thread.list.length < sw.workers }.to be_true
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "#stop and #start" do
|
33
|
+
let(:sw) { SilentWorker.new {|data| data } }
|
34
|
+
|
35
|
+
after { sw.abort }
|
36
|
+
|
37
|
+
it "jobs won't be fired when stopping workers" do
|
38
|
+
sw << 1
|
39
|
+
sw.stop
|
40
|
+
sw << 2
|
41
|
+
sw.queue.pop.should == 2
|
42
|
+
end
|
43
|
+
|
44
|
+
it "resume works when #start called" do
|
45
|
+
sw = SilentWorker.new {|data| data }
|
46
|
+
sw << 1
|
47
|
+
sw.stop
|
48
|
+
sw << 2
|
49
|
+
sw.queue.length.should == 1
|
50
|
+
sw.start
|
51
|
+
sw.wait
|
52
|
+
sw.queue.length.should == 0
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -- coding: utf-8
|
2
|
+
|
3
|
+
require "rubygems"
|
4
|
+
|
5
|
+
if ENV["COVERAGE"]
|
6
|
+
require "simplecov"
|
7
|
+
SimpleCov.start
|
8
|
+
else
|
9
|
+
require 'coveralls'
|
10
|
+
Coveralls.wear!
|
11
|
+
end
|
12
|
+
|
13
|
+
require "bundler/setup"
|
14
|
+
Bundler.require :default, :test
|
15
|
+
require "rspec-expectations"
|
16
|
+
require "rspec/matchers/built_in/be"
|
17
|
+
|
18
|
+
Dir["./spec/support/**/*.rb"].each{|file| require file }
|
19
|
+
|
20
|
+
require File.expand_path("../../lib/silent_worker.rb", __FILE__)
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: silent_worker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- uu59
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-23 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: rspec
|
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: SilentWorker gives simple worker thread model
|
42
|
+
email:
|
43
|
+
- k@uu59.org
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- .travis.yml
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/silent_worker.rb
|
55
|
+
- lib/silent_worker/version.rb
|
56
|
+
- silent_worker.gemspec
|
57
|
+
- spec/silent_worker_spec.rb
|
58
|
+
- spec/spec_helper.rb
|
59
|
+
homepage: ''
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata: {}
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 2.0.3
|
80
|
+
signing_key:
|
81
|
+
specification_version: 4
|
82
|
+
summary: SilentWorker gives simple worker thread model
|
83
|
+
test_files:
|
84
|
+
- spec/silent_worker_spec.rb
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
has_rdoc:
|