concurrent_worker 0.2.3 → 0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c743d7ebe52dcc21dcb90529e65d3cd0bbadf66a29dbbe16f8245f3c764cb787
4
- data.tar.gz: f6d17a27e1b0666e896ebfeecf1b618ed2d2be87f5e9801899b3387ed4619469
3
+ metadata.gz: 86b8b4753276bc1539a0e517043df93fde982ecf42aa2a57ca6758cd1a9642ed
4
+ data.tar.gz: e76f854ea2cb78a4adff9338631b7381ed6e5d79a3aac780c44461a334cbd07c
5
5
  SHA512:
6
- metadata.gz: 8843c5397cfd752204733a1b5f51f7fa9e924c5a7074ccb41b40f32320ef9a69680b16f1bd65dc9deec731f3ea155c3a8b832faf143499df9dc53525dfaf8d85
7
- data.tar.gz: 979d6dd58b496f055a0647f8ff2a531e209c309d7475589998918c09e287b01b031bf0cabc16c7ba12e03dce91448ede61648469f0b55cf5fae8796fb3d6a811
6
+ metadata.gz: 7c664e6c43c4022da34de9b4f2f45474ad87d09d82bc5f4f2e6eb63423e5c66a433d84dca54941d437b0d8270f0c824c054476cda239b4bc6e50bebfb237ce7b
7
+ data.tar.gz: 019b80e4ad2a553e4ccdce6d63962da6e2ccaddd3b63d21ab664e55b39c58673d58bc75b8b95bab54dd326e10a632a54b23d9b78aba21deb4639069fa363cf83
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- concurrent_worker (0.2.3)
4
+ concurrent_worker (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -29,7 +29,7 @@ require 'concurrent_worker'
29
29
  Thread.abort_on_exception = true
30
30
 
31
31
  # define a work block.
32
- logger = ConcurrentWorker::Worker.new do |args|
32
+ logger = ConcurrentWorker::Worker.new do |*args|
33
33
  printf(*args)
34
34
  $stdout.flush
35
35
  nil
@@ -45,7 +45,7 @@ logger.join
45
45
  If you need some preparation for the work block, you can define 'base block'.
46
46
 
47
47
  ```ruby
48
- logger = ConcurrentWorker::Worker.new do |args|
48
+ logger = ConcurrentWorker::Worker.new do |*args|
49
49
  # work block and base block can share object with instance variable(@xxx).
50
50
  printf(@file, *args)
51
51
  @file.flush
@@ -1,3 +1,3 @@
1
1
  module ConcurrentWorker
2
- VERSION = "0.2.3"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -3,6 +3,7 @@ require "concurrent_worker/version"
3
3
  module ConcurrentWorker
4
4
  class Error < StandardError; end
5
5
 
6
+ require 'thread'
6
7
 
7
8
  class RequestCounter
8
9
  def initialize
@@ -13,14 +14,16 @@ module ConcurrentWorker
13
14
  @count.push(args)
14
15
  end
15
16
  def pop
16
- @count.pop
17
- @com.push([])
17
+ Thread.handle_interrupt(Object => :never) do
18
+ @count.pop
19
+ @com.push(true)
20
+ end
18
21
  end
19
22
 
20
- def wait(n)
21
- return if @count.size <= n
23
+ def wait_until_less_than(n)
24
+ return if @count.size < n
22
25
  while @com.pop
23
- break if @count.size <= n
26
+ break if @count.size < n
24
27
  end
25
28
  end
26
29
  def size
@@ -49,7 +52,6 @@ module ConcurrentWorker
49
52
 
50
53
 
51
54
 
52
- require 'thread'
53
55
  class Worker
54
56
  attr_accessor :channel
55
57
  # Worker : worker class
@@ -106,10 +108,13 @@ module ConcurrentWorker
106
108
  raise "block is nil" unless callback
107
109
  @result_callbacks.push(callback)
108
110
  end
111
+ def clear_callbacks
112
+ @result_callbacks.clear
113
+ end
109
114
 
110
115
  def call_result_callbacks(args)
111
116
  @result_callbacks.each do |callback|
112
- callback.call(args)
117
+ callback.call(*args)
113
118
  end
114
119
  @req_counter.pop
115
120
  end
@@ -118,6 +123,9 @@ module ConcurrentWorker
118
123
  raise "block is nil" unless callback
119
124
  @retired_callbacks.push(callback)
120
125
  end
126
+ def clear_retired_callbacks
127
+ @retired_callbacks.clear
128
+ end
121
129
 
122
130
  def call_retired_callbacks
123
131
  @retired_callbacks.each do |callback|
@@ -155,7 +163,7 @@ module ConcurrentWorker
155
163
  if work_block
156
164
  set_block(:work_block, &work_block)
157
165
  end
158
- send_res(yield_work_block(args))
166
+ send_res(yield_work_block(*args))
159
167
  end
160
168
  end
161
169
  end
@@ -168,8 +176,8 @@ module ConcurrentWorker
168
176
 
169
177
  def run
170
178
  @state = :run
171
- set_default_loop_block unless @loop_block
172
- set_default_base_block unless @base_block
179
+ set_default_loop_block unless defined?(@loop_block) && @loop_block
180
+ set_default_base_block unless defined?(@base_block) && @base_block
173
181
  cncr_block
174
182
  end
175
183
 
@@ -177,7 +185,7 @@ module ConcurrentWorker
177
185
  unless @state == :run
178
186
  run
179
187
  end
180
- @req_counter.wait(@snd_queue_max)
188
+ @req_counter.wait_until_less_than(@snd_queue_max)
181
189
  begin
182
190
  @req_counter.push([args, work_block])
183
191
  send_req([args, work_block])
@@ -189,7 +197,6 @@ module ConcurrentWorker
189
197
 
190
198
  def quit
191
199
  begin
192
- @req_counter.push([])
193
200
  send_req([])
194
201
  true
195
202
  rescue ClosedQueueError, IOError
@@ -198,7 +205,7 @@ module ConcurrentWorker
198
205
  end
199
206
 
200
207
  def join
201
- @req_counter.wait(0)
208
+ @req_counter.wait_until_less_than(1)
202
209
  quit
203
210
  wait_cncr_proc
204
211
  end
@@ -368,7 +375,7 @@ module ConcurrentWorker
368
375
  loop do
369
376
  break if (result = @rcv_queue.pop).empty?
370
377
  @result_callbacks.each do |callback|
371
- callback.call(result[0])
378
+ callback.call(*result[0])
372
379
  end
373
380
  @req_counter.pop
374
381
  end
@@ -411,8 +418,9 @@ module ConcurrentWorker
411
418
 
412
419
 
413
420
  def deploy_worker
421
+ defined?(@work_block) || @work_block = nil
414
422
  w = Worker.new(*@args, type: @options[:type], snd_queue_max: @snd_queue_max, &@work_block)
415
- w.add_callback do |arg|
423
+ w.add_callback do |*arg|
416
424
  @rcv_queue.push([arg])
417
425
  @ready_queue.push(w)
418
426
  end
@@ -438,13 +446,13 @@ module ConcurrentWorker
438
446
  end
439
447
 
440
448
  def req(*args, &work_block)
441
- @req_counter.wait(@max_num * @snd_queue_max)
449
+ @req_counter.wait_until_less_than(@max_num * @snd_queue_max)
442
450
  @req_counter.push(true)
443
451
  @snd_queue.push([args, work_block])
444
452
  end
445
453
 
446
454
  def join
447
- @req_counter.wait(0)
455
+ @req_counter.wait_until_less_than(1)
448
456
  self.shift.join until self.empty?
449
457
  @rcv_queue.push([])
450
458
  @rcv_thread.join
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: concurrent_worker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - dddogdiamond
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-08 00:00:00.000000000 Z
11
+ date: 2019-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler