in_threads 1.1.2 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/README.markdown +2 -0
- data/in_threads.gemspec +1 -1
- data/lib/in_threads/enumerable.rb +26 -0
- data/lib/in_threads.rb +19 -33
- metadata +6 -9
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
OTlmNWM3Y2U1MGJiOTZlMzAwOTA1ODk3MjRkZmM1OTc3ZWFkNmUyOA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ODcxOTQ5NjcxNGVjMDg1MzdkNzFlYWU4MjkwNjNhMDg2MzE3ZTE3Yg==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NDIxYzg5YzY1OWIxYWJiMDAzMDBiNmU3ZTliYTkzNTZkMDA3ZDgxZGMyN2Nm
|
10
|
+
MDdlYWVlZTYxNDc2ZjJlNDE0M2M2MTQ4OWZmNDQ3MGRjODBhOWQxMTkzOTkz
|
11
|
+
OTRiY2NhZmEwNmY0OTI3ZWMyYjRjODEzNTkyYmM2NzAxNGU4YjM=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZjUxYTMyYzdjNzVjYzU1YTQ5NTBmOWEwYmRlMjViMWM3M2ZhZjlhMWQyZmZk
|
14
|
+
YzBmMWI1NGM4NWVlYjc1ZTNhZjdiMjgzN2QxY2I3ZmQ3MGQ5YjhkZWNlNDE2
|
15
|
+
OWRmYzgzNTg4N2VmY2EwYmMzZWU2ZmRkYjQ5MTVkN2IzNTg4MDc=
|
data/README.markdown
CHANGED
data/in_threads.gemspec
CHANGED
@@ -0,0 +1,26 @@
|
|
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.rb
CHANGED
@@ -1,41 +1,12 @@
|
|
1
1
|
require 'thread'
|
2
|
+
require 'delegate'
|
2
3
|
|
3
|
-
|
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
|
27
|
-
|
28
|
-
class InThreads
|
29
|
-
(
|
30
|
-
instance_methods.map(&:to_s) -
|
31
|
-
%w[__id__ __send__ class inspect instance_of? is_a? kind_of? nil? object_id respond_to? send]
|
32
|
-
).each{ |name| undef_method name }
|
33
|
-
(private_instance_methods.map(&:to_s) - %w[initialize raise method_missing]).each{ |name| undef_method name }
|
34
|
-
|
4
|
+
class InThreads < Delegator
|
35
5
|
attr_reader :enumerable, :thread_count
|
36
6
|
def initialize(enumerable, thread_count = 10, &block)
|
7
|
+
super(enumerable)
|
37
8
|
@enumerable, @thread_count = enumerable, thread_count.to_i
|
38
|
-
unless enumerable.
|
9
|
+
unless enumerable.is_a?(Enumerable)
|
39
10
|
raise ArgumentError.new('`enumerable` should include Enumerable.')
|
40
11
|
end
|
41
12
|
if thread_count < 2
|
@@ -109,8 +80,21 @@ class InThreads
|
|
109
80
|
end
|
110
81
|
end
|
111
82
|
|
83
|
+
# befriend with progress gem
|
84
|
+
def with_progress(title = nil, length = nil, &block)
|
85
|
+
::Progress::WithProgress.new(self, title, length, &block)
|
86
|
+
end
|
87
|
+
|
112
88
|
protected
|
113
89
|
|
90
|
+
def __getobj__
|
91
|
+
@enumerable
|
92
|
+
end
|
93
|
+
|
94
|
+
def __setobj__(obj)
|
95
|
+
@enumerable = obj
|
96
|
+
end
|
97
|
+
|
114
98
|
autoload :ThreadLimiter, 'in_threads/thread_limiter'
|
115
99
|
autoload :Filler, 'in_threads/filler'
|
116
100
|
|
@@ -161,3 +145,5 @@ protected
|
|
161
145
|
enumerable.send(method, *args, &block)
|
162
146
|
end
|
163
147
|
end
|
148
|
+
|
149
|
+
require 'in_threads/enumerable'
|
metadata
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: in_threads
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.2.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Ivan Kuchin
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-08-
|
11
|
+
date: 2013-08-20 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rspec
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ! '>='
|
20
18
|
- !ruby/object:Gem::Version
|
@@ -22,7 +20,6 @@ dependencies:
|
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ! '>='
|
28
25
|
- !ruby/object:Gem::Version
|
@@ -40,6 +37,7 @@ files:
|
|
40
37
|
- README.markdown
|
41
38
|
- in_threads.gemspec
|
42
39
|
- lib/in_threads.rb
|
40
|
+
- lib/in_threads/enumerable.rb
|
43
41
|
- lib/in_threads/filler.rb
|
44
42
|
- lib/in_threads/thread_limiter.rb
|
45
43
|
- spec/in_threads_spec.rb
|
@@ -47,27 +45,26 @@ files:
|
|
47
45
|
homepage: http://github.com/toy/in_threads
|
48
46
|
licenses:
|
49
47
|
- MIT
|
48
|
+
metadata: {}
|
50
49
|
post_install_message:
|
51
50
|
rdoc_options: []
|
52
51
|
require_paths:
|
53
52
|
- lib
|
54
53
|
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
-
none: false
|
56
54
|
requirements:
|
57
55
|
- - ! '>='
|
58
56
|
- !ruby/object:Gem::Version
|
59
57
|
version: '0'
|
60
58
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
-
none: false
|
62
59
|
requirements:
|
63
60
|
- - ! '>='
|
64
61
|
- !ruby/object:Gem::Version
|
65
62
|
version: '0'
|
66
63
|
requirements: []
|
67
64
|
rubyforge_project: in_threads
|
68
|
-
rubygems_version:
|
65
|
+
rubygems_version: 2.0.3
|
69
66
|
signing_key:
|
70
|
-
specification_version:
|
67
|
+
specification_version: 4
|
71
68
|
summary: Execute ruby code in parallel
|
72
69
|
test_files:
|
73
70
|
- spec/in_threads_spec.rb
|