parallel_server 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/parallel_server/prefork.rb +29 -33
  3. metadata +3 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c5d5502b45b7905a8987e8ca85d33718086a57bc
4
- data.tar.gz: 9cba1d1da00b7b52d335b615ff0c54d52a56eb45
3
+ metadata.gz: 53f28860a0d7d10e6e37f34c79c612b451fb1071
4
+ data.tar.gz: 73f2343779c6db95c2444fef17b207a883e463ad
5
5
  SHA512:
6
- metadata.gz: 01383431223dc5812e135947fbb7d98ad5cfefa88009c408e9e51d20a82bb8a7db48cfc81e458918ee65be98b8fa921f65291ef9848d82ec7165d7c553961807
7
- data.tar.gz: 42202bdc861f0cc9e95b0acf9d01e38ea81540385d1a39bee34ec657333dd3bada3dcd82f7582d26eff1d4c518a7c6dcb07b3c05146bd29464a11aaad25e7d64
6
+ metadata.gz: 1f8cada5754f3dd15479e5b9be04dff99d3da70b4bccea3ef3ee848c7e1f3d56c25f86234cac44e9f3147df6cf9fed93d9595ec75515d610c04eae9fa868b64f
7
+ data.tar.gz: 3df445fb613a4a12e3f145d7e89937e5848c48145d5cfe467345884a748a5d93c8048806bc681410e61a7f25ca4440c32e6a80d2ec9a77dc9c905802bfcecbcf
@@ -38,7 +38,6 @@ module ParallelServer
38
38
  @to_child = {} # pid => IO
39
39
  @child_status = {} # pid => Hash
40
40
  @children = [] # pid
41
- @thread_to_child = {} # pid => Thread
42
41
  @loop = true
43
42
  end
44
43
 
@@ -62,8 +61,6 @@ module ParallelServer
62
61
  @sockets.each{|s| s.close rescue nil} if @sockets
63
62
  @to_child.values.each{|s| s.close rescue nil}
64
63
  @to_child.clear
65
- @thread_to_child.values.each(&:exit)
66
- @thread_to_child.clear
67
64
  Timeout.timeout(1){wait_all_children} rescue Thread.new{wait_all_children}
68
65
  end
69
66
 
@@ -120,24 +117,28 @@ module ParallelServer
120
117
  # @param data [String]
121
118
  # @return [void]
122
119
  def talk_to_children(data)
123
- @data_to_child = Marshal.dump(data)
124
- @thread_to_child.values.each do |thr|
125
- begin
126
- thr.run
127
- rescue ThreadError
128
- # try to run dead thread. ignore it.
129
- end
120
+ data_to_child = Marshal.dump(data)
121
+ each_nonblock(@to_child.values, 1) do |io|
122
+ Conversation._send(io, data_to_child) rescue nil
130
123
  end
131
124
  end
132
125
 
133
- # @param io [IO]
134
- # @return [void]
135
- def talk_to_child_loop(io)
136
- data = nil
137
- while true
138
- Thread.stop if data.nil? || data == @data_to_child
139
- data = @data_to_child
140
- Conversation._send(io, data)
126
+ # @param values [Array]
127
+ # @param timeout [Numeric]
128
+ # @yield [obj]
129
+ # @yieldparam obj [Object] one of values
130
+ def each_nonblock(values, timeout)
131
+ values = values.dup
132
+ until values.empty?
133
+ thr = Thread.new do
134
+ until values.empty? || Thread.current[:exit]
135
+ value = values.shift
136
+ break unless value
137
+ yield value
138
+ end
139
+ end
140
+ thr.join(timeout)
141
+ thr[:exit] = true
141
142
  end
142
143
  end
143
144
 
@@ -172,15 +173,11 @@ module ParallelServer
172
173
  if st[:status] == :stop
173
174
  @to_child[pid].close rescue nil
174
175
  @to_child.delete pid
175
- @thread_to_child[pid].exit rescue nil
176
- @thread_to_child.delete pid
177
176
  end
178
177
  else
179
178
  @from_child.delete from_child
180
179
  @to_child[pid].close rescue nil
181
180
  @to_child.delete pid
182
- @thread_to_child[pid].exit rescue nil
183
- @thread_to_child.delete pid
184
181
  @child_status.delete pid
185
182
  from_child.close
186
183
  end
@@ -250,7 +247,6 @@ module ParallelServer
250
247
  to_child[0].close
251
248
  @from_child[from_child[0]] = pid
252
249
  @to_child[pid] = to_child[1]
253
- @thread_to_child[pid] = Thread.new(to_child[1]){|io| talk_to_child_loop(io)}
254
250
  @children.push pid
255
251
  @child_status[pid] = {status: :run, connections: {}}
256
252
  @on_child_start.call(pid) if @on_child_start
@@ -305,12 +301,12 @@ module ParallelServer
305
301
  # @param block [#call]
306
302
  # @return [void]
307
303
  def start(block)
308
- main_thread = Thread.current
309
- accept_thread = Thread.new{ accept_loop(block, main_thread) }
310
- reload_thread = Thread.new{ reload_loop(main_thread) }
304
+ queue = Queue.new
305
+ accept_thread = Thread.new{ accept_loop(block, queue) }
306
+ reload_thread = Thread.new{ reload_loop(queue) }
311
307
 
312
308
  # wait that accept_loop or reload_loop end
313
- Thread.stop while @status == :run
309
+ queue.pop
314
310
 
315
311
  accept_thread.exit
316
312
  @sockets.each(&:close)
@@ -324,9 +320,9 @@ module ParallelServer
324
320
  private
325
321
 
326
322
  # @param block [#call]
327
- # @param main_thread [Thread]
323
+ # @param queue [Queue]
328
324
  # @return [void]
329
- def accept_loop(block, main_thread)
325
+ def accept_loop(block, queue)
330
326
  count = 0
331
327
  while @status == :run
332
328
  wait_thread
@@ -342,7 +338,7 @@ module ParallelServer
342
338
  end
343
339
  ensure
344
340
  @status = :stop
345
- main_thread.run
341
+ queue.push true
346
342
  end
347
343
 
348
344
  # @return [void]
@@ -353,9 +349,9 @@ module ParallelServer
353
349
  @status = :exit
354
350
  end
355
351
 
356
- # @param main_thread [Thread]
352
+ # @param queue [Queue]
357
353
  # @return [void]
358
- def reload_loop(main_thread)
354
+ def reload_loop(queue)
359
355
  while true
360
356
  data = Conversation.recv(@from_parent)
361
357
  break unless data
@@ -368,7 +364,7 @@ module ParallelServer
368
364
  @from_parent = nil
369
365
  ensure
370
366
  @status = :stop
371
- main_thread.run
367
+ queue.push true
372
368
  end
373
369
 
374
370
  # @return [void]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parallel_server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomita Masahiro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-06 00:00:00.000000000 Z
11
+ date: 2015-03-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Parallel TCP Server library. This is easy to make Multi-Process / Multi-Thread
14
14
  server
@@ -40,9 +40,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
40
40
  version: '0'
41
41
  requirements: []
42
42
  rubyforge_project:
43
- rubygems_version: 2.2.2
43
+ rubygems_version: 2.4.5
44
44
  signing_key:
45
45
  specification_version: 4
46
46
  summary: Parallel TCP Server library
47
47
  test_files: []
48
- has_rdoc: true