in_threads 1.2.2 → 1.3.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 +6 -14
- data/.rubocop.yml +53 -0
- data/.travis.yml +11 -1
- data/Gemfile +1 -1
- data/README.markdown +6 -2
- data/in_threads.gemspec +4 -1
- data/lib/in_threads.rb +153 -44
- data/spec/in_threads_spec.rb +277 -202
- data/spec/spec_helper.rb +0 -0
- metadata +22 -9
- data/lib/in_threads/enumerable.rb +0 -26
- data/lib/in_threads/filler.rb +0 -54
- data/lib/in_threads/thread_limiter.rb +0 -41
data/spec/spec_helper.rb
ADDED
File without changes
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: in_threads
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ivan Kuchin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rubocop
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.26.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.26.0
|
27
41
|
description:
|
28
42
|
email:
|
29
43
|
executables: []
|
@@ -31,16 +45,15 @@ extensions: []
|
|
31
45
|
extra_rdoc_files: []
|
32
46
|
files:
|
33
47
|
- .gitignore
|
48
|
+
- .rubocop.yml
|
34
49
|
- .travis.yml
|
35
50
|
- Gemfile
|
36
51
|
- LICENSE.txt
|
37
52
|
- README.markdown
|
38
53
|
- in_threads.gemspec
|
39
54
|
- lib/in_threads.rb
|
40
|
-
- lib/in_threads/enumerable.rb
|
41
|
-
- lib/in_threads/filler.rb
|
42
|
-
- lib/in_threads/thread_limiter.rb
|
43
55
|
- spec/in_threads_spec.rb
|
56
|
+
- spec/spec_helper.rb
|
44
57
|
homepage: http://github.com/toy/in_threads
|
45
58
|
licenses:
|
46
59
|
- MIT
|
@@ -51,20 +64,20 @@ require_paths:
|
|
51
64
|
- lib
|
52
65
|
required_ruby_version: !ruby/object:Gem::Requirement
|
53
66
|
requirements:
|
54
|
-
- -
|
67
|
+
- - '>='
|
55
68
|
- !ruby/object:Gem::Version
|
56
69
|
version: '0'
|
57
70
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
71
|
requirements:
|
59
|
-
- -
|
72
|
+
- - '>='
|
60
73
|
- !ruby/object:Gem::Version
|
61
74
|
version: '0'
|
62
75
|
requirements: []
|
63
76
|
rubyforge_project: in_threads
|
64
|
-
rubygems_version: 2.
|
77
|
+
rubygems_version: 2.4.2
|
65
78
|
signing_key:
|
66
79
|
specification_version: 4
|
67
80
|
summary: Execute ruby code in parallel
|
68
81
|
test_files:
|
69
82
|
- spec/in_threads_spec.rb
|
70
|
-
|
83
|
+
- spec/spec_helper.rb
|
@@ -1,26 +0,0 @@
|
|
1
|
-
require 'in_threads'
|
2
|
-
|
3
|
-
module Enumerable
|
4
|
-
# Run enumerable method blocks in threads
|
5
|
-
#
|
6
|
-
# urls.in_threads.map do |url|
|
7
|
-
# url.fetch
|
8
|
-
# end
|
9
|
-
#
|
10
|
-
# Specify number of threads to use:
|
11
|
-
#
|
12
|
-
# files.in_threads(4).all? do |file|
|
13
|
-
# file.valid?
|
14
|
-
# end
|
15
|
-
#
|
16
|
-
# Passing block runs it against <tt>each</tt>
|
17
|
-
#
|
18
|
-
# urls.in_threads.each{ … }
|
19
|
-
#
|
20
|
-
# is same as
|
21
|
-
#
|
22
|
-
# urls.in_threads{ … }
|
23
|
-
def in_threads(thread_count = 10, &block)
|
24
|
-
InThreads.new(self, thread_count, &block)
|
25
|
-
end
|
26
|
-
end
|
data/lib/in_threads/filler.rb
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
require 'thread'
|
2
|
-
|
3
|
-
class InThreads
|
4
|
-
class Filler
|
5
|
-
class Extractor
|
6
|
-
include Enumerable
|
7
|
-
|
8
|
-
def initialize(filler)
|
9
|
-
@filler = filler
|
10
|
-
@queue = []
|
11
|
-
end
|
12
|
-
|
13
|
-
def push(o)
|
14
|
-
@queue.push(o)
|
15
|
-
end
|
16
|
-
|
17
|
-
def each
|
18
|
-
begin
|
19
|
-
loop do
|
20
|
-
while @filler.synchronize{ @queue.empty? }
|
21
|
-
@filler.run
|
22
|
-
end
|
23
|
-
yield @filler.synchronize{ @queue.shift }
|
24
|
-
end
|
25
|
-
rescue ThreadError => e
|
26
|
-
end
|
27
|
-
nil # non reusable
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
attr_reader :extractors
|
32
|
-
def initialize(enum, extractor_count)
|
33
|
-
@extractors = Array.new(extractor_count){ Extractor.new(self) }
|
34
|
-
@mutex = Mutex.new
|
35
|
-
@filler = Thread.new do
|
36
|
-
enum.each do |o|
|
37
|
-
synchronize do
|
38
|
-
@extractors.each do |extractor|
|
39
|
-
extractor.push(o)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def run
|
47
|
-
@filler.run
|
48
|
-
end
|
49
|
-
|
50
|
-
def synchronize(&block)
|
51
|
-
@mutex.synchronize(&block)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
require 'thwait'
|
2
|
-
|
3
|
-
class InThreads
|
4
|
-
# Use ThreadsWait to limit number of threads
|
5
|
-
class ThreadLimiter
|
6
|
-
# Initialize with limit
|
7
|
-
def initialize(count)
|
8
|
-
@count = count
|
9
|
-
@waiter = ThreadsWait.new
|
10
|
-
end
|
11
|
-
|
12
|
-
# Without block behaves as <tt>new</tt>
|
13
|
-
# With block yields it with <tt>self</tt> and ensures running of <tt>finalize</tt>
|
14
|
-
def self.limit(count, &block)
|
15
|
-
limiter = new(count)
|
16
|
-
if block
|
17
|
-
begin
|
18
|
-
yield limiter
|
19
|
-
ensure
|
20
|
-
limiter.finalize
|
21
|
-
end
|
22
|
-
else
|
23
|
-
limiter
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
# Add thread to <tt>ThreadsWait</tt>, wait for finishing of one thread if limit reached
|
28
|
-
def <<(thread)
|
29
|
-
if @waiter.threads.length + 1 >= @count
|
30
|
-
@waiter.join(thread).join
|
31
|
-
else
|
32
|
-
@waiter.join_nowait(thread)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
# Wait for waiting threads
|
37
|
-
def finalize
|
38
|
-
@waiter.all_waits(&:join)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|