jparallel 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e8a7a5ec17084fa8f1e62c9a49d01f3d66095a02
4
- data.tar.gz: 6cb3d56a35a38f80862b5d1a4a92609b26e9dcd7
3
+ metadata.gz: a11c348627ed9d42fbbfc7c171a2de4c189be5e7
4
+ data.tar.gz: 9220a634ebe628b29dcc0b4bdcfcf9240179e40b
5
5
  SHA512:
6
- metadata.gz: a72d330ba4d063f3aa7f20b2da8b585110eb9980fb0efce5be87cd56e140dee23ea81d3ecf3543d326016e8aaa7bf00972d4edc56e1b8ccdedf521d8014fea13
7
- data.tar.gz: ca7c38de5315d38385a4951f361eb716f575af2986efa018e3200e67d9b1ecc5b774bb4efb2e407dea466c96d79152ec49d6cf36baf1823214c3275da57800af
6
+ metadata.gz: 1bd0cc6fbfca372d77634ab190b0dd00453bf2ab7b65414406641463d6a5c0c00a5274f7d2f8be009d25db6387334afd9d71c2ebfcb1a2d956b3b26172f3d65c
7
+ data.tar.gz: 048ba129144b8b22d40e51dce874d73a30b3bb29e8918337daad6374bc9b363e32604c87c1d0f6ef722218105c93f3eb5cfa5cf053bc0e3db3aa5bc4d99c4243
data/Gemfile.lock CHANGED
@@ -1,59 +1,59 @@
1
1
  GEM
2
2
  remote: https://rubygems.org/
3
3
  specs:
4
- celluloid (0.15.2)
5
- timers (~> 1.1.0)
6
- celluloid-io (0.15.0)
7
- celluloid (>= 0.15.0)
8
- nio4r (>= 0.5.0)
4
+ celluloid (0.16.0)
5
+ timers (~> 4.0.0)
9
6
  coderay (1.1.0)
10
7
  diff-lcs (1.2.5)
11
- ffi (1.9.3)
12
- ffi (1.9.3-java)
13
- formatador (0.2.4)
14
- guard (2.4.0)
8
+ ffi (1.9.6-java)
9
+ formatador (0.2.5)
10
+ guard (2.10.4)
15
11
  formatador (>= 0.2.4)
16
- listen (~> 2.1)
12
+ listen (~> 2.7)
17
13
  lumberjack (~> 1.0)
14
+ nenv (~> 0.1)
18
15
  pry (>= 0.9.12)
19
16
  thor (>= 0.18.1)
20
- guard-rspec (4.2.7)
17
+ guard-compat (1.1.0)
18
+ guard-rspec (4.5.0)
21
19
  guard (~> 2.1)
22
- rspec (>= 2.14, < 4.0)
23
- listen (2.6.0)
20
+ guard-compat (~> 1.1)
21
+ rspec (>= 2.99.0, < 4.0)
22
+ hitimes (1.2.2-java)
23
+ listen (2.8.4)
24
24
  celluloid (>= 0.15.2)
25
- celluloid-io (>= 0.15.0)
26
25
  rb-fsevent (>= 0.9.3)
27
26
  rb-inotify (>= 0.9)
28
- lumberjack (1.0.4)
27
+ lumberjack (1.0.9)
29
28
  method_source (0.8.2)
30
- nio4r (1.0.0-java)
31
- pry (0.9.12.6)
32
- coderay (~> 1.0)
33
- method_source (~> 0.8)
34
- slop (~> 3.4)
35
- pry (0.9.12.6-java)
36
- coderay (~> 1.0)
37
- method_source (~> 0.8)
29
+ nenv (0.1.1)
30
+ pry (0.10.1-java)
31
+ coderay (~> 1.1.0)
32
+ method_source (~> 0.8.1)
38
33
  slop (~> 3.4)
39
34
  spoon (~> 0.0)
40
35
  rb-fsevent (0.9.4)
41
- rb-inotify (0.9.3)
36
+ rb-inotify (0.9.5)
42
37
  ffi (>= 0.5.0)
43
- rspec (2.14.1)
44
- rspec-core (~> 2.14.0)
45
- rspec-expectations (~> 2.14.0)
46
- rspec-mocks (~> 2.14.0)
47
- rspec-core (2.14.7)
48
- rspec-expectations (2.14.5)
49
- diff-lcs (>= 1.1.3, < 2.0)
50
- rspec-mocks (2.14.6)
51
- slop (3.4.7)
38
+ rspec (3.1.0)
39
+ rspec-core (~> 3.1.0)
40
+ rspec-expectations (~> 3.1.0)
41
+ rspec-mocks (~> 3.1.0)
42
+ rspec-core (3.1.7)
43
+ rspec-support (~> 3.1.0)
44
+ rspec-expectations (3.1.2)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.1.0)
47
+ rspec-mocks (3.1.3)
48
+ rspec-support (~> 3.1.0)
49
+ rspec-support (3.1.2)
50
+ slop (3.6.0)
52
51
  spoon (0.0.4)
53
52
  ffi
54
- thor (0.18.1)
55
- thread (0.1.3)
56
- timers (1.1.0)
53
+ thor (0.19.1)
54
+ thread (0.1.4)
55
+ timers (4.0.1)
56
+ hitimes
57
57
 
58
58
  PLATFORMS
59
59
  java
data/README.md CHANGED
@@ -38,5 +38,15 @@ jp.hashmap({a: 1, b: 2}) {|x,y| "#{x} -> #{y}" }
38
38
  # -> {:a=>"a -> 1", :b=>"b -> 2"}
39
39
  ```
40
40
 
41
+ ### Timeouts
42
+ With all the methods you can add an optional timeout value in seconds. This timeout is checked before executing the block for each element of the input collection. If the timeout is reached before we begin working on a work-unit, then the `TimeoutError` with the index or key of the item is placed in the output collection.
43
+ ```ruby
44
+ jp.map((0..20).to_a, timeout: 0.1) do |x|
45
+ sleep(0.1)
46
+ x
47
+ end
48
+ # -> [0,1, ... ,TimeoutError, TimeoutError]
49
+ ```
50
+
41
51
  ## Licence
42
52
  This library is licenced under the GNU Lesser General Public Licence, version 3 or later.
data/jparallel.gemspec CHANGED
@@ -1,12 +1,15 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "jparallel"
3
- s.version = "0.1.1"
3
+ s.version = "0.1.3"
4
4
  s.license = "LGPL-3.0"
5
5
  s.summary = "Easy parallel processing for ruby"
6
6
  s.files = `git ls-files`.split("\n")
7
7
  s.require_paths = ["lib"]
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = ["Ravi Desai"]
10
+ s.add_dependency "thread"
11
+ s.add_dependency "pry"
12
+ s.add_dependency "rspec"
10
13
 
11
14
  s.homepage = "https://github.com/ravster/jparallel"
12
15
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
data/lib/jparallel.rb CHANGED
@@ -30,15 +30,22 @@ class Jparallel
30
30
  output_array.map(&:value)
31
31
  end
32
32
 
33
- def map_with_index (input_array, &block)
33
+ def map_with_index (input_array, ops={}, &block)
34
+ timeout = ops[:timeout]
34
35
  output_array = Array.new input_array.size
36
+ start_time = Time.now
35
37
 
36
38
  input_array.each_with_index do |item, index|
37
39
  output_array[index] = Thread.future @pool do
38
- begin
39
- yield(item, index)
40
- rescue => e
41
- e
40
+ if !timeout.nil? &&
41
+ (Time.now - start_time) > timeout
42
+ TimeoutError.new("Timed out before item #{index}")
43
+ else
44
+ begin
45
+ yield(item, index)
46
+ rescue => e
47
+ e
48
+ end
42
49
  end
43
50
  end
44
51
  end
@@ -46,15 +53,22 @@ class Jparallel
46
53
  output_array.map(&:value)
47
54
  end
48
55
 
49
- def hashmap (input_hash, &block)
56
+ def hashmap (input_hash, ops={}, &block)
57
+ timeout = ops[:timeout]
50
58
  output_hash = {}
59
+ start_time = Time.now
51
60
 
52
61
  input_hash.each_pair do |key, value|
53
62
  output_hash[key] = Thread.future @pool do
54
- begin
55
- yield(key, value)
56
- rescue => e
57
- e
63
+ if !timeout.nil? &&
64
+ (Time.now - start_time) > timeout
65
+ TimeoutError.new("Timed out before item #{key}")
66
+ else
67
+ begin
68
+ yield(key, value)
69
+ rescue => e
70
+ e
71
+ end
58
72
  end
59
73
  end
60
74
  end
@@ -26,5 +26,21 @@ describe Jparallel do
26
26
  it "returns errors within the hash" do
27
27
  expect(error_result[:a]).to be_a StandardError
28
28
  end
29
+
30
+ let(:timeout_hash_result) do
31
+ big_array = (0..20).to_a.map do |x|
32
+ [x,x]
33
+ end
34
+ big_hash = Hash[big_array]
35
+ jp.hashmap(big_hash, timeout: 0.01) do |k,v|
36
+ sleep(0.01)
37
+ v
38
+ end
39
+ end
40
+
41
+ it "returns timeouts in the output" do
42
+ expect(timeout_hash_result.any? {|k,v| v.is_a?(TimeoutError)}).
43
+ to be(true)
44
+ end
29
45
  end
30
46
  end
@@ -23,21 +23,20 @@ describe Jparallel do
23
23
  jp.map(error_array) { |x| 12 / x }
24
24
  end
25
25
 
26
- it "returns an array even when there is an exception in one of the input" do
27
- expect(error_array_result).to be_instance_of Array
28
- end
29
-
30
26
  it "stores the exception in the result" do
31
27
  expect(error_array_result.last).to be_a StandardError
32
28
  end
33
29
 
34
- let(:timeout_array_result) do
35
- jp.map((0..20).to_a, timeout: 0.1) { |x| sleep(0.1) }
30
+ let(:timeout_array_map_result) do
31
+ jp.map((0..20).to_a, timeout: 0.01) do |x|
32
+ sleep(0.01)
33
+ x
34
+ end
36
35
  end
37
36
 
38
37
  it "returns timeouts in the output" do
39
- expect(timeout_array_result.any?{ |x| x.is_a?(TimeoutError)}).
38
+ expect(timeout_array_map_result.any?{ |x| x.is_a?(TimeoutError)}).
40
39
  to be(true)
41
- end
40
+ end
42
41
  end
43
42
  end
@@ -18,7 +18,7 @@ describe Jparallel do
18
18
  input_array.each_with_index.map do |x, index|
19
19
  "#{x} is at index #{index}"
20
20
  end
21
- )
21
+ )
22
22
  end
23
23
 
24
24
  let(:error_array_result) do
@@ -34,5 +34,14 @@ describe Jparallel do
34
34
  it "returns errors within the array" do
35
35
  expect(error_array_result.last).to be_a StandardError
36
36
  end
37
+
38
+ it "returns timeouts in the output" do
39
+ result = jp.map_with_index((0..20).to_a, timeout: 0.001) do |x, index|
40
+ sleep(0.001)
41
+ x
42
+ end
43
+ expect(result.any? {|x| x.is_a?(TimeoutError)}).
44
+ to be(true)
45
+ end
37
46
  end
38
47
  end
metadata CHANGED
@@ -1,15 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jparallel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ravi Desai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-15 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2014-12-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thread
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ requirement: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - '>='
23
+ - !ruby/object:Gem::Version
24
+ version: '0'
25
+ prerelease: false
26
+ type: :runtime
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ prerelease: false
40
+ type: :runtime
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ version_requirements: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirement: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ prerelease: false
54
+ type: :runtime
13
55
  description:
14
56
  email:
15
57
  executables: []
@@ -51,7 +93,4 @@ rubygems_version: 2.1.9
51
93
  signing_key:
52
94
  specification_version: 4
53
95
  summary: Easy parallel processing for ruby
54
- test_files:
55
- - spec/jparallel_hashmap_spec.rb
56
- - spec/jparallel_map_spec.rb
57
- - spec/jparallel_map_with_index_spec.rb
96
+ test_files: []