puli 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 04fb0e60b54cd86ecccb9f0b152af0f44aa0a296
4
+ data.tar.gz: 388010ba590d7ae221a25e69fb226639d2b85f4b
5
+ SHA512:
6
+ metadata.gz: cc02b0fd0c578e8197df1673e72f08690de310f4327ee63719da16f0bcf33f87c2e6253dd25a4db17af43e2491ad6466cec9cf6fb733ac7fba1a97d52e296f71
7
+ data.tar.gz: a7a0b6182e9db8f0f622c0fa456cbcc1450945c4eacc6897d21b07a3cbc10df14cff3c09d261d4b67269e6f094efcadc480db320f13017101a1c1a37cdacce47
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ group :development do
4
+ gem "rspec", "~> 3.2", "< 3.3"
5
+ gem "rdoc", "~> 3.12"
6
+ gem "bundler", "~> 1.0"
7
+ gem "jeweler", "~> 2", ">= 2.1.2"
8
+ gem "simplecov", ">= 0"
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2016 Julik Tarkhanov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ A fluffy corded and threaded pool. The smallest thread pool available on the market to date.
2
+ The Puli is enumerable, mappable, parallel and also _tiny._
3
+
4
+ To execute lots of things in parallel:
5
+
6
+ ```ruby
7
+ puli = Puli.new(num_threads: 4, tasks: uris)
8
+ response_bodies = puli.map do |uri_task|
9
+ Patron::Session.new.get(uri_task).body
10
+ end
11
+ ```
12
+
13
+ You can spool tasks one by one as well
14
+
15
+ ```ruby
16
+ puli = Puli.new(num_threads: 4)
17
+ database.each_row do |row|
18
+ puli << row
19
+ end
20
+
21
+ puli.each do |database_row|
22
+ output.write(database_row)
23
+ end
24
+ ```
25
+
26
+ If an exception is raised during the execution of a task, Puli will make sure other threads stop before starting
27
+ their next iteration:
28
+
29
+ ```ruby
30
+ puli = Puli.new(num_threads: 4, tasks: five_thousand_heavy_items)
31
+ puli.each do |item|
32
+ #..... raise TimeoutError
33
+ end
34
+ # Will not have executed all the five thousand heavy items, just some,
35
+ # and will also raise TimeoutError up into the caller, regardless of whether
36
+ # Thread.abort_on_exception is set to true.
37
+ ```
38
+
39
+ ## Contributing to puli
40
+
41
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
42
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
43
+ * Fork the project.
44
+ * Start a feature/bugfix branch.
45
+ * Commit and push until you are happy with your contribution.
46
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
47
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
48
+
49
+ ## Copyright
50
+
51
+ Copyright (c) 2016 Julik Tarkhanov. See LICENSE.txt for
52
+ further details.
53
+
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
17
+ gem.name = "puli"
18
+ gem.homepage = "http://github.com/julik/puli"
19
+ gem.license = "MIT"
20
+ gem.description = 'For small concurrent activities in the open air'
21
+ gem.summary = 'A tiny thread pool'
22
+ gem.email = "me@julik.nl"
23
+ gem.authors = ["Julik Tarkhanov"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ desc "Code coverage detail"
35
+ task :simplecov do
36
+ ENV['COVERAGE'] = "true"
37
+ Rake::Task['spec'].execute
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'rdoc/task'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "puli #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/lib/puli.rb ADDED
@@ -0,0 +1,37 @@
1
+ require 'thread'
2
+
3
+ class Puli
4
+ include Enumerable
5
+
6
+ def initialize(num_threads: 3, tasks: [])
7
+ @num_threads = num_threads.to_i
8
+ @q = Queue.new
9
+ tasks.map{|t| @q << t }
10
+ end
11
+
12
+ def <<(task)
13
+ @q << task
14
+ end
15
+
16
+ def each
17
+ last_captured_error = false
18
+ threads = (1..@num_threads.to_i).map do
19
+ Thread.new do
20
+ loop do
21
+ break if last_captured_error
22
+ begin
23
+ task = @q.pop(non_block=true)
24
+ yield(task)
25
+ rescue ThreadError
26
+ break # Queue emptied
27
+ rescue Exception => e
28
+ last_captured_error = e
29
+ end
30
+ end
31
+ end
32
+ end
33
+ threads.map(&:join)
34
+ raise last_captured_error if last_captured_error
35
+ self
36
+ end
37
+ end
data/puli.gemspec ADDED
@@ -0,0 +1,63 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: puli 1.0.0 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "puli"
9
+ s.version = "1.0.0"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib"]
13
+ s.authors = ["Julik Tarkhanov"]
14
+ s.date = "2016-11-11"
15
+ s.description = "For small concurrent activities in the open air"
16
+ s.email = "me@julik.nl"
17
+ s.extra_rdoc_files = [
18
+ "LICENSE.txt",
19
+ "README.md"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".rspec",
24
+ "Gemfile",
25
+ "LICENSE.txt",
26
+ "README.md",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "lib/puli.rb",
30
+ "puli.gemspec",
31
+ "spec/puli_spec.rb",
32
+ "spec/spec_helper.rb"
33
+ ]
34
+ s.homepage = "http://github.com/julik/puli"
35
+ s.licenses = ["MIT"]
36
+ s.rubygems_version = "2.4.5.1"
37
+ s.summary = "A tiny thread pool"
38
+
39
+ if s.respond_to? :specification_version then
40
+ s.specification_version = 4
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_development_dependency(%q<rspec>, ["< 3.3", "~> 3.2"])
44
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
45
+ s.add_development_dependency(%q<bundler>, ["~> 1.0"])
46
+ s.add_development_dependency(%q<jeweler>, [">= 2.1.2", "~> 2"])
47
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
48
+ else
49
+ s.add_dependency(%q<rspec>, ["< 3.3", "~> 3.2"])
50
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
51
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
52
+ s.add_dependency(%q<jeweler>, [">= 2.1.2", "~> 2"])
53
+ s.add_dependency(%q<simplecov>, [">= 0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<rspec>, ["< 3.3", "~> 3.2"])
57
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
58
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
59
+ s.add_dependency(%q<jeweler>, [">= 2.1.2", "~> 2"])
60
+ s.add_dependency(%q<simplecov>, [">= 0"])
61
+ end
62
+ end
63
+
data/spec/puli_spec.rb ADDED
@@ -0,0 +1,53 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe 'Puli' do
4
+ it 'executes all the queued tasks' do
5
+ out = StringIO.new
6
+ mux = Mutex.new
7
+ p = Puli.new(num_threads: 25)
8
+ 500.times {|i|
9
+ p << "This is task #{i}"
10
+ }
11
+ p.each do | task_string |
12
+ sleep(rand * 0.8)
13
+ mux.synchronize { out.puts(task_string) }
14
+ end
15
+
16
+ results = out.string
17
+ sorted_results = results.split("\n").reject(&:empty?).sort
18
+ expect(sorted_results.length).to eq(500)
19
+ end
20
+
21
+ it 'raises the exception and stops executing the tasks on the first exception' do
22
+ 20.times do
23
+ p = Puli.new(num_threads: 4)
24
+ 1000.times {|i| p << i }
25
+ processed = []
26
+ expect {
27
+ p.each do |t|
28
+ raise "This is wrong" if rand > 0.8
29
+ processed << t
30
+ end
31
+ }.to raise_error(/This is wrong/)
32
+ expect(processed.length).to be < 300
33
+ end
34
+ end
35
+
36
+ it 'allows mapping over the result' do
37
+ p = Puli.new(num_threads: 4)
38
+ 20.times {|i| p << i }
39
+ results = p.map{|task| task ** 2 }
40
+ sorted = results.sort
41
+ expect(sorted.length).to eq(20)
42
+ expect(sorted[-1]).to eq(19 ** 2)
43
+ end
44
+
45
+ it 'supports the tasks[] kwarg' do
46
+ p = Puli.new(num_threads: 4, tasks: (1..100))
47
+ results = p.map{|task| task + 1 }
48
+ sorted = results.sort
49
+ expect(sorted.length).to eq(100)
50
+ expect(sorted[0]).to eq(1 + 1)
51
+ expect(sorted[-1]).to eq(100 + 1)
52
+ end
53
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'rspec'
5
+ require 'puli'
6
+
7
+ RSpec.configure do |config|
8
+ config.order = 'random'
9
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: puli
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Julik Tarkhanov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "<"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.3'
20
+ - - "~>"
21
+ - !ruby/object:Gem::Version
22
+ version: '3.2'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "<"
28
+ - !ruby/object:Gem::Version
29
+ version: '3.3'
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '3.2'
33
+ - !ruby/object:Gem::Dependency
34
+ name: rdoc
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.12'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.12'
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: jeweler
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 2.1.2
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: '2'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 2.1.2
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '2'
81
+ - !ruby/object:Gem::Dependency
82
+ name: simplecov
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ description: For small concurrent activities in the open air
96
+ email: me@julik.nl
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files:
100
+ - LICENSE.txt
101
+ - README.md
102
+ files:
103
+ - ".document"
104
+ - ".rspec"
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - VERSION
110
+ - lib/puli.rb
111
+ - puli.gemspec
112
+ - spec/puli_spec.rb
113
+ - spec/spec_helper.rb
114
+ homepage: http://github.com/julik/puli
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.4.5.1
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: A tiny thread pool
138
+ test_files: []