concurrent_worker 0.4.0 → 0.4.1

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: 4d77bf3d8370938048821167d1f8c5a08096c5e91b9f47ffc142d7d93275520e
4
- data.tar.gz: eebfcd996c0b334777bf7592811113fb7acbb9db541b514e4285ee80a064f6c3
3
+ metadata.gz: 695c62e55f8cbedfffed63cee4fc4f6adfe8f975002406878af4b804ae051304
4
+ data.tar.gz: 3cc2f1074a15fc9d3826d04752262efb251fcdd19183608b8b233737b8f76951
5
5
  SHA512:
6
- metadata.gz: 69d87367f6edb44ae9ddec0e55df7cde86b94c201334a9a24a2a67b9180cf466f79794b3d8d375ace39d51ba57dcc2c34d61b514110fb08eaf7d9259cedfb2c9
7
- data.tar.gz: ea3455877fba65f7879f6f8408d79cc69e9bb6e8395e1ae86cb48e20b737b52776d067a4804b5da906ee68dfc99da1d15cd01637c8dcb9d0718ba00f0eb99c12
6
+ metadata.gz: 5b6921bc9aafd1f7a20888a084cb425e5c3e65a3caa60ff2ddb937aed6084129c5f4060ecc0870ac55843351ecad6133bbee036ff1805743513f3af705b9bf9a
7
+ data.tar.gz: fea9f25a9715d7c19145c12736259f65e72dc942eae369d0125236ba25b96c2e68b7da66fbbb747b090360800d92cc2774a40befc99f013b82b485a7c291c38d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- concurrent_worker (0.4.0)
4
+ concurrent_worker (0.4.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -12,8 +12,9 @@ module ConcurrentWorker
12
12
  end
13
13
  def pop
14
14
  Thread.handle_interrupt(Object => :never) do
15
- @count.pop
15
+ r = @count.pop
16
16
  @com.push(true)
17
+ r
17
18
  end
18
19
  end
19
20
 
@@ -23,6 +24,11 @@ module ConcurrentWorker
23
24
  break if @count.size < n
24
25
  end
25
26
  end
27
+
28
+ def empty?
29
+ @count.empty?
30
+ end
31
+
26
32
  def size
27
33
  @count.size
28
34
  end
@@ -35,15 +41,6 @@ module ConcurrentWorker
35
41
  @count.closed?
36
42
  end
37
43
 
38
- def rest
39
- result = []
40
- until @count.empty?
41
- req = @count.pop
42
- next if req == []
43
- result.push(req)
44
- end
45
- result
46
- end
47
44
  end
48
45
 
49
46
  class IPCDuplexChannel
@@ -61,23 +58,14 @@ module ConcurrentWorker
61
58
 
62
59
  def send(obj)
63
60
  begin
64
- Thread.handle_interrupt(Object => :never) do
65
- data = Marshal.dump(obj)
66
- @wio.write([data.size].pack("I"))
67
- @wio.write(data)
68
- end
61
+ Marshal.dump(obj, @wio)
69
62
  rescue Errno::EPIPE
70
63
  end
71
64
  end
72
65
 
73
66
  def recv
74
67
  begin
75
- Thread.handle_interrupt(Object => :on_blocking) do
76
- szdata = @rio.read(4)
77
- return [] if szdata.nil?
78
- size = szdata.unpack("I")[0]
79
- Marshal.load(@rio.read(size))
80
- end
68
+ Marshal.load(@rio)
81
69
  rescue IOError
82
70
  raise StopIteration
83
71
  end
@@ -1,3 +1,3 @@
1
1
  module ConcurrentWorker
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.1"
3
3
  end
@@ -1,6 +1,5 @@
1
1
  module ConcurrentWorker
2
2
  class Worker
3
- attr_accessor :channel
4
3
  # Worker : worker class
5
4
  # +cncr_block : concurrent processing block : thread(as ConcurrentThread)/process(as ConcurrentProcess)
6
5
  # +base_block : user defined preparation to exec 'work block'
@@ -11,7 +10,7 @@ module ConcurrentWorker
11
10
  # so that they can share instance variables:@xxxx.
12
11
  #
13
12
 
14
- attr_reader :req_counter, :snd_queue_max
13
+ attr_reader :req_counter, :snd_queue_max, :undone_requests
15
14
 
16
15
  def queue_closed?
17
16
  @req_counter.closed?
@@ -20,7 +19,7 @@ module ConcurrentWorker
20
19
  !queue_closed? && @req_counter.size == 0
21
20
  end
22
21
  def queue_available?
23
- !queue_closed? && @req_counter.size < @snd_queue_max
22
+ !queue_closed? && ( @snd_queue_max == 0 || @req_counter.size < @snd_queue_max )
24
23
  end
25
24
 
26
25
  def initialize(*args, **options, &work_block)
@@ -35,7 +34,8 @@ module ConcurrentWorker
35
34
  @snd_queue_max = @options[:snd_queue_max] || 2
36
35
  @req_counter = RequestCounter.new
37
36
  @options[:result_callback_interrupt] ||= :immediate
38
- @options[:retired_callback_interrupt] ||= :immediate
37
+ @options[:retired_callback_interrupt] ||= :immediate
38
+ @undone_requests = []
39
39
 
40
40
  case @options[:type]
41
41
  when :process
@@ -87,6 +87,13 @@ module ConcurrentWorker
87
87
  end
88
88
  end
89
89
  end
90
+
91
+ def req_counter_close
92
+ @req_counter.close
93
+ until @req_counter.empty?
94
+ @undone_requests.push(@req_counter.pop)
95
+ end
96
+ end
90
97
 
91
98
  def result_handle_thread(&recv_block)
92
99
  Thread.new do
@@ -96,7 +103,7 @@ module ConcurrentWorker
96
103
  recv_block.call
97
104
  end
98
105
  ensure
99
- @req_counter.close
106
+ req_counter_close
100
107
  channel_close
101
108
  call_retired_callbacks
102
109
  end
@@ -162,7 +169,7 @@ module ConcurrentWorker
162
169
  unless @state == :run
163
170
  run
164
171
  end
165
- @req_counter.wait_until_less_than(@snd_queue_max)
172
+ @req_counter.wait_until_less_than(@snd_queue_max) if @snd_queue_max > 0
166
173
  begin
167
174
  @req_counter.push([args, work_block])
168
175
  send_req([args, work_block])
@@ -186,11 +193,12 @@ module ConcurrentWorker
186
193
 
187
194
  def join
188
195
  unless @state == :run
189
- return
196
+ return true
190
197
  end
191
198
  @req_counter.wait_until_less_than(1)
192
199
  quit
193
200
  wait_cncr_proc
201
+ true
194
202
  end
195
203
  end
196
204
 
@@ -1,18 +1,35 @@
1
1
  module ConcurrentWorker
2
2
  class WorkerPool < Array
3
+ class ReadyWorkerQueue < Queue
4
+ alias :super_push :push
5
+ alias :super_pop :pop
6
+ def initialize
7
+ super()
8
+ @m = Mutex.new
9
+ end
10
+ def push(arg)
11
+ @m.synchronize do
12
+ super_push(arg)
13
+ end
14
+ end
15
+ def pop
16
+ @m.synchronize do
17
+ queued = []
18
+ queued.push(super_pop) until empty?
19
+ queued.sort_by{ |w| w.req_counter.size }.each{ |w| super_push(w) }
20
+ end
21
+ super_pop
22
+ end
23
+ end
24
+
3
25
 
4
26
  def need_new_worker?
5
27
  self.size < @max_num && self.select{ |w| w.queue_empty? }.empty?
6
28
  end
7
29
 
8
30
  def available_worker
9
- delete_if{ |w| w.queue_closed? }
10
- if need_new_worker?
11
- w = deploy_worker
12
- w.snd_queue_max.times do
13
- @ready_queue.push(w)
14
- end
15
- end
31
+ delete_if{ |w| w.queue_closed? && w.join }
32
+ deploy_worker if need_new_worker?
16
33
  @ready_queue.pop
17
34
  end
18
35
 
@@ -47,7 +64,7 @@ module ConcurrentWorker
47
64
  @set_blocks.push([:work_block, work_block])
48
65
  end
49
66
 
50
- @ready_queue = Queue.new
67
+ @ready_queue = ReadyWorkerQueue.new
51
68
 
52
69
  @result_callbacks = []
53
70
 
@@ -87,17 +104,20 @@ module ConcurrentWorker
87
104
  end
88
105
 
89
106
  w.add_retired_callback do
90
- w.req_counter.rest.each do
107
+ w.undone_requests.each do
91
108
  |req|
92
109
  @snd_queue.push(req)
93
110
  end
94
- @ready_queue.push(w)
95
111
  end
96
112
 
97
113
  @set_blocks.each do |symbol, block|
98
114
  w.set_block(symbol, &block)
99
115
  end
100
116
  w.run
117
+
118
+ w.snd_queue_max.times do
119
+ @ready_queue.push(w)
120
+ end
101
121
  self.push(w)
102
122
  w
103
123
  end
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.4.0
4
+ version: 0.4.1
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-12 00:00:00.000000000 Z
11
+ date: 2019-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler