easy_threadpool 0.0.2
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 +15 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +46 -0
- data/Rakefile +1 -0
- data/easy_threadpool.gemspec +28 -0
- data/examples/sample.rb +23 -0
- data/lib/easy_threadpool.rb +10 -0
- data/lib/easy_threadpool/pool.rb +61 -0
- data/lib/easy_threadpool/version.rb +3 -0
- metadata +85 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YjcxNmE2YmZiNTQzMGNhMWJjMzk5N2Y0OGVkMTE2NmIwMDY0YzFmMw==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZmVlMTM2NDQwYmVjZmUwZGUyNDZmYWY2NzU5NDFjYTg5MGQ5MDRiOA==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YWNhZTMzMjQ3MGEyZmNjMzg1NzdkMGZmMzc0ODcxN2JhZTU0MzJiM2E0OGEy
|
10
|
+
NTQyMWJlZGNkZmEzN2Q4YTEwNDkxMmVlODhiYWE5NWJmNDk3ZWFjNzA0ZjE3
|
11
|
+
NTZlZGQxZWY4ZDI2YzFkZWI0Yzc5ZjcxNWIwMmRkNTI0MWJiOWI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NDFjZTkwOTA0NjBkNWRjODVjNGQ4ODAzMWIwOWMxMGNjZmJkYThkNTEwZjVl
|
14
|
+
MTEzOTEwZGIyMWRjOThlYWZhOWU1NDEzZWEzYWMyN2E2NmExYWIyNjEwZjIw
|
15
|
+
NTYxM2Y0YmUzOTljNWQ0ZmFhNGEzNzk5MjM0M2U3NDJhZWZjYTU=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Francesco Laurita
|
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,46 @@
|
|
1
|
+
# EasyThreadpool
|
2
|
+
|
3
|
+
This gem has been written keeping semplicity in mind
|
4
|
+
|
5
|
+
It implements Thread Pool Pattern described here http://en.wikipedia.org/wiki/Thread_pool_pattern
|
6
|
+
|
7
|
+
Recommended for I/O activities are easily parallelizable, such as downloading remote resources, web scraping, bulk API calls, etc
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'easy_threadpool'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install easy_threadpool
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require "easy_threadpool"
|
27
|
+
pool = Thread.pool(10)
|
28
|
+
|
29
|
+
20.times {
|
30
|
+
pool.process do
|
31
|
+
# Put your slow task here
|
32
|
+
sleep rand(0.3..2)
|
33
|
+
puts "Task consumed by worker ##{Thread.current[:id]}"
|
34
|
+
end
|
35
|
+
}
|
36
|
+
pool.shutdown
|
37
|
+
|
38
|
+
```
|
39
|
+
|
40
|
+
## Contributing
|
41
|
+
|
42
|
+
1. Fork it
|
43
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
44
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
45
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
46
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'easy_threadpool/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "easy_threadpool"
|
8
|
+
spec.version = EasyThreadpool::VERSION
|
9
|
+
spec.authors = ["Francesco Laurita"]
|
10
|
+
spec.email = ["francesco.laurita@gmail.com"]
|
11
|
+
spec.description = %q{An easy to use Thread Pool implementation}
|
12
|
+
spec.summary = %q{
|
13
|
+
This gem has been written keeping semplicity in mind
|
14
|
+
It implements Thread Pool Pattern described here http://en.wikipedia.org/wiki/Thread_pool_pattern
|
15
|
+
Recommended for I/O activities are easily parallelizable, such as downloading
|
16
|
+
remote resources, web scraping, bulk API calls, etc
|
17
|
+
}
|
18
|
+
spec.homepage = "https://github.com/taganaka/easy_threadpool_rb"
|
19
|
+
spec.license = "MIT"
|
20
|
+
|
21
|
+
spec.files = `git ls-files`.split($/)
|
22
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
23
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
27
|
+
spec.add_development_dependency "rake"
|
28
|
+
end
|
data/examples/sample.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
lib = File.expand_path('../../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "easy_threadpool"
|
4
|
+
require "open-uri"
|
5
|
+
require "logger"
|
6
|
+
|
7
|
+
@logger = Logger.new(STDOUT)
|
8
|
+
|
9
|
+
def download(url)
|
10
|
+
open(url).read
|
11
|
+
@logger.info "Url #{url} fetched by worker ##{Thread.current[:id]}"
|
12
|
+
end
|
13
|
+
|
14
|
+
pool = Thread.pool(10)
|
15
|
+
urls = []
|
16
|
+
open("https://rubygems.org/gems?letter=A").read.scan(/a href="(\/gems\/.*)"/) { |match| urls << "https://rubygems.org#{match.first}"}
|
17
|
+
urls.each do |u|
|
18
|
+
pool.process u do |url|
|
19
|
+
download url
|
20
|
+
end
|
21
|
+
end
|
22
|
+
pool.shutdown
|
23
|
+
@logger.info "Consumed #{pool.task_consumed} tasks by #{pool.size} workers"
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require "thread"
|
2
|
+
module EasyThreadpool
|
3
|
+
class Pool
|
4
|
+
|
5
|
+
# Creating a new ThreadPool of size equal to @size
|
6
|
+
# It defines how many threads we will have
|
7
|
+
# spawned internally.
|
8
|
+
def initialize(size = 10)
|
9
|
+
# Pool size
|
10
|
+
@size = size
|
11
|
+
# Queue holds tasks
|
12
|
+
@queue = Queue.new
|
13
|
+
# Threads are created
|
14
|
+
@pool = Array.new(size) { |i| worker i}
|
15
|
+
|
16
|
+
@consumed = 0
|
17
|
+
end
|
18
|
+
|
19
|
+
# It schedules a job.
|
20
|
+
# The block of code will be executed once a thread is available
|
21
|
+
def process(*args, &block)
|
22
|
+
@queue << [block, args]
|
23
|
+
end
|
24
|
+
|
25
|
+
# Graceful shutdown
|
26
|
+
# Always call pool.shutdow to ensure all of your enqueud
|
27
|
+
# task are executed
|
28
|
+
def shutdown
|
29
|
+
1.upto(@size) {|i| @queue << [:EXIT]}
|
30
|
+
@pool.map(&:join)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Force shutdown
|
34
|
+
def shutdown!
|
35
|
+
@pool.map(&:exit)
|
36
|
+
end
|
37
|
+
|
38
|
+
def task_consumed
|
39
|
+
@consumed
|
40
|
+
end
|
41
|
+
|
42
|
+
def size
|
43
|
+
@size
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
# It creates a task consumer
|
48
|
+
# :EXIT is a special internal message to handle Graceful shutdown
|
49
|
+
def worker id
|
50
|
+
Thread.new do
|
51
|
+
Thread.current[:id] = id
|
52
|
+
while true do
|
53
|
+
code, args = @queue.pop
|
54
|
+
break if code == :EXIT
|
55
|
+
@consumed += 1
|
56
|
+
code.call(*args)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy_threadpool
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Francesco Laurita
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-20 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: An easy to use Thread Pool implementation
|
42
|
+
email:
|
43
|
+
- francesco.laurita@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- easy_threadpool.gemspec
|
54
|
+
- examples/sample.rb
|
55
|
+
- lib/easy_threadpool.rb
|
56
|
+
- lib/easy_threadpool/pool.rb
|
57
|
+
- lib/easy_threadpool/version.rb
|
58
|
+
homepage: https://github.com/taganaka/easy_threadpool_rb
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.0.7
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: This gem has been written keeping semplicity in mind It implements Thread
|
82
|
+
Pool Pattern described here http://en.wikipedia.org/wiki/Thread_pool_pattern Recommended
|
83
|
+
for I/O activities are easily parallelizable, such as downloading remote resources,
|
84
|
+
web scraping, bulk API calls, etc
|
85
|
+
test_files: []
|