threadify_procs 0.0.4 → 0.0.5

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: bb372e84fc599f94d64457028fa111f204a78ee4
4
- data.tar.gz: 8f75d617fc0f75fa6461332eb51f34dbee82d315
3
+ metadata.gz: fc57f9e5501434f13db1118db4dfef1236f1a064
4
+ data.tar.gz: 26780ccbe180a22d820b6191ec3b9423d74adc2b
5
5
  SHA512:
6
- metadata.gz: 60ce1e33aad21018b394347d0d515296dcb4dd69e4a0cbc678d757263dd9ac0518edd94e29cdb0aaf4bae5767d21c26bee100749b7b4f1ca9f3361649c49671f
7
- data.tar.gz: 7f45bc5b363bd8abb97b05bb5f70aa543d38cc3a359ac9dcefa73357a7c370958543a88aaedd8d22af1eeef58e4f2b2bdfe2378e5054f108e44e32c358fde50a
6
+ metadata.gz: bd7c174e094c5e6d2be6263d4d75e1df338837a5b64b9336ce8cd91e46e715b58971d9329c7e8bd50a7d104ce12d7f05bc6425327ca597a87daab57a2f14ab1b
7
+ data.tar.gz: 0a0c937875041e7fd1d289d0b5713aefb0536f50a0c5dabf94af4576b2d9e1198f936ccd69bf2c1fec4d5694900c40960bcb6742c5fb807d57c15b81776a0ba1
data/.gitignore CHANGED
@@ -15,3 +15,6 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+
19
+ .idea/
20
+ .ruby-version
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # ThreadifyProcs
2
2
 
3
+ [![Build Status](https://travis-ci.org/aq/threadify_procs.svg?branch=master)](https://travis-ci.org/aq/threadify_procs)
4
+
3
5
  Create an array of Procs, launch them within threads. It adresses the common
4
6
  problem of writting files concurrently in threads with a ruby proces or
5
7
  downloading simultaneously multiple files. It avoids 'Too many open files'
@@ -8,6 +8,7 @@ module ThreadifyProcs
8
8
  set_options options
9
9
  with_writer_thread do
10
10
  launch_procs
11
+ if @callback; @callback.call end
11
12
  end
12
13
  end
13
14
 
@@ -64,6 +65,7 @@ module ThreadifyProcs
64
65
  end
65
66
  @procs_per_thread = (@procs.size / @number_of_threads.to_f).ceil
66
67
  @with_writer = !!options[:with_writer]
68
+ @callback = options[:callback]
67
69
  end
68
70
 
69
71
  def with_writer_thread
@@ -1,3 +1,3 @@
1
1
  module ThreadifyProcs
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -5,22 +5,25 @@ class ThreadifiedJob
5
5
  include ThreadifyProcs
6
6
  attr_reader :total
7
7
 
8
- def initialize
8
+ def initialize(options={})
9
+ @options = { number_of_threads: 2 }.merge options
9
10
  @total = 0
10
11
  end
11
12
 
13
+ def executed_function; end
12
14
  def procs
13
15
  [].tap do |_procs|
14
16
  3.times do |n|
15
17
  _procs << Proc.new do
16
18
  @total += n+1
19
+ executed_function
17
20
  end
18
21
  end
19
22
  end
20
23
  end
21
24
 
22
25
  def launch
23
- call_with_threads procs, number_of_threads: 2
26
+ call_with_threads procs, @options
24
27
  end
25
28
  end
26
29
 
@@ -46,20 +49,34 @@ describe ThreadifyProcs do
46
49
  let(:tmp_dir) { "#{File.dirname(__FILE__)}/../tmp" }
47
50
 
48
51
  describe 'call_with_threads' do
49
- subject{ ThreadifiedJob.new }
52
+ subject{ job.launch }
50
53
 
51
- it 'should create threads from procs' do
52
- subject.launch
53
- expect(subject.total).to eq 6
54
+ context 'with 2 threads' do
55
+ let(:job) { ThreadifiedJob.new }
56
+ it 'should create threads from procs' do
57
+ subject
58
+ expect(job.total).to eq 6
59
+ end
60
+ end
61
+
62
+ context 'with callback' do
63
+ let(:job) do
64
+ ThreadifiedJob.new(callback: Proc.new { Struct.new(:success) })
65
+ end
66
+ it 'should call the callback after the procs' do
67
+ expect(job).to receive(:executed_function).exactly(3).times.ordered
68
+ expect(Struct).to receive(:new).with(:success).ordered
69
+ subject
70
+ end
54
71
  end
55
72
 
56
- describe 'with_writer' do
57
- subject{ ThreadifiedJobWithWriter.new }
73
+ context 'with_writer' do
74
+ let(:job) { ThreadifiedJobWithWriter.new }
58
75
  before { FileUtils.mkdir tmp_dir}
59
76
  after { FileUtils.rm_r tmp_dir}
60
77
 
61
78
  it 'should create threads from procs' do
62
- subject.launch
79
+ subject
63
80
 
64
81
  3.times do |n|
65
82
  path = "#{tmp_dir}/#{n}.txt"
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: threadify_procs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Antoine Qu'hen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-04 00:00:00.000000000 Z
11
+ date: 2014-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.5'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.5'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  description:
@@ -59,8 +59,8 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - .gitignore
63
- - .travis.yml
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
64
  - Gemfile
65
65
  - LICENSE.txt
66
66
  - README.md
@@ -80,17 +80,17 @@ require_paths:
80
80
  - lib
81
81
  required_ruby_version: !ruby/object:Gem::Requirement
82
82
  requirements:
83
- - - '>='
83
+ - - ">="
84
84
  - !ruby/object:Gem::Version
85
85
  version: '0'
86
86
  required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  requirements:
88
- - - '>='
88
+ - - ">="
89
89
  - !ruby/object:Gem::Version
90
90
  version: '0'
91
91
  requirements: []
92
92
  rubyforge_project:
93
- rubygems_version: 2.4.1
93
+ rubygems_version: 2.2.2
94
94
  signing_key:
95
95
  specification_version: 4
96
96
  summary: Launch an array of Procs within threads.