concurrent_worker 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/concurrent_worker.gemspec +30 -0
- data/lib/concurrent_worker/version.rb +3 -0
- data/lib/concurrent_worker.rb +292 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 678beb9ed4ddb2d3c4595a4b286e6ebd195a4c5f645460619f5725af3d3064a7
|
4
|
+
data.tar.gz: 6877a4d045f8dc820175bed7ada367a553ac4be85db3390903b7fbb50291ab53
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7215dd0d8019ee6e20ff3416ec9efff7f5ead7c4144b92b8cf45e43234c96de8189651d23db08a3384d1e55676c0ad7e5216e5da611eb7d1456d568ad0129a8c
|
7
|
+
data.tar.gz: b62ec465175412dede959aa285b4610b43b20b7e843d107f893318576b06decef8f41ff9ec57dd76be6189f7bf612b758563e13e629447443a51fb24c5df6d9a
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 MURATA Takahiro
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# ConcurrentWorker
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/concurrent_worker`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'concurrent_worker'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install concurrent_worker
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/concurrent_worker.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "concurrent_worker"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "concurrent_worker/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "concurrent_worker"
|
8
|
+
spec.version = ConcurrentWorker::VERSION
|
9
|
+
spec.authors = ["mur"]
|
10
|
+
spec.email = ["mur@mur.jp"]
|
11
|
+
|
12
|
+
spec.summary = %q{Concurrent worker in thread/process with preparation structure.}
|
13
|
+
spec.description = %q{I will write later...}
|
14
|
+
spec.homepage = "https://github.com/murjp/concurrent_worker"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
spec.bindir = "exe"
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
30
|
+
end
|
@@ -0,0 +1,292 @@
|
|
1
|
+
require "concurrent_worker/version"
|
2
|
+
|
3
|
+
module ConcurrentWorker
|
4
|
+
class Error < StandardError; end
|
5
|
+
|
6
|
+
require 'thread'
|
7
|
+
class Worker
|
8
|
+
attr_accessor :channel
|
9
|
+
# Worker : worker class
|
10
|
+
# +cncr_block : concurrent processing block : thread(as ConcurrentThread)/process(as ConcurrentProcess)
|
11
|
+
# +base_block : user defined preparation to exec 'work block'
|
12
|
+
# +loop_block : loop of receiving request and exec 'looped block'
|
13
|
+
# +work_block : user requested work
|
14
|
+
#
|
15
|
+
# These blocks are executed with 'instance_exec' method of worker,
|
16
|
+
# so that they can share instance variables:@xxxx.
|
17
|
+
#
|
18
|
+
|
19
|
+
attr_reader :req_queue, :req_queue_max
|
20
|
+
|
21
|
+
def initialize(*args, **options, &work_block)
|
22
|
+
@args = args
|
23
|
+
@options = options
|
24
|
+
set_block(:work_block, &work_block) if work_block
|
25
|
+
|
26
|
+
@state = :idle
|
27
|
+
@callbacks = []
|
28
|
+
|
29
|
+
@req_queue_max = @options[ :req_queue_max ] || 2
|
30
|
+
@req_queue = SizedQueue.new(@req_queue_max)
|
31
|
+
|
32
|
+
if @options[ :type ] == :process
|
33
|
+
Worker.include ConcurrentProcess
|
34
|
+
else
|
35
|
+
Worker.include ConcurrentThread
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def add_callback(&callback)
|
40
|
+
raise "block is nil" unless callback
|
41
|
+
@callbacks.push(callback)
|
42
|
+
end
|
43
|
+
|
44
|
+
def call_callbacks(args)
|
45
|
+
@callbacks.each do |callback|
|
46
|
+
callback.call(args)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def set_block(symbol, &block)
|
51
|
+
raise "block is nil" unless block
|
52
|
+
|
53
|
+
unless [:base_block, :loop_block, :work_block].include?(symbol)
|
54
|
+
raise symbol.to_s + " is not used as worker block"
|
55
|
+
end
|
56
|
+
|
57
|
+
worker_block = Proc.new do |*args|
|
58
|
+
self.instance_exec(*args, &block)
|
59
|
+
end
|
60
|
+
instance_variable_set("@" + symbol.to_s, worker_block)
|
61
|
+
|
62
|
+
define_singleton_method("yield_" + symbol.to_s) do |*args|
|
63
|
+
blk = instance_variable_get("@" + symbol.to_s)
|
64
|
+
if blk
|
65
|
+
blk.call(*args)
|
66
|
+
else
|
67
|
+
raise "block " + symbol.to_s + " is not defined"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def run
|
73
|
+
@state = :run
|
74
|
+
|
75
|
+
unless @loop_block
|
76
|
+
set_block(:loop_block) do
|
77
|
+
loop do
|
78
|
+
break if (req = receive_req).empty?
|
79
|
+
(args, work_block) = req
|
80
|
+
if work_block
|
81
|
+
set_block(:work_block, &work_block)
|
82
|
+
end
|
83
|
+
send_res(yield_work_block(args))
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
unless @base_block
|
89
|
+
set_block(:base_block) do
|
90
|
+
yield_loop_block
|
91
|
+
end
|
92
|
+
end
|
93
|
+
cncr_block
|
94
|
+
end
|
95
|
+
|
96
|
+
|
97
|
+
def req(*args, &work_block)
|
98
|
+
unless @state == :run
|
99
|
+
run
|
100
|
+
end
|
101
|
+
send_req([args, work_block])
|
102
|
+
end
|
103
|
+
|
104
|
+
def quit
|
105
|
+
send_req([])
|
106
|
+
end
|
107
|
+
|
108
|
+
def join
|
109
|
+
quit
|
110
|
+
wait_cncr_proc
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
|
115
|
+
module ConcurrentThread
|
116
|
+
def cncr_block
|
117
|
+
@thread_channel = Queue.new
|
118
|
+
@thread = Thread.new do
|
119
|
+
yield_base_block
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def send_req(args)
|
124
|
+
@req_queue.push(args)
|
125
|
+
@thread_channel.push(args)
|
126
|
+
end
|
127
|
+
|
128
|
+
def receive_req
|
129
|
+
@thread_channel.pop
|
130
|
+
end
|
131
|
+
|
132
|
+
def send_res(args)
|
133
|
+
call_callbacks(args)
|
134
|
+
@req_queue.pop
|
135
|
+
end
|
136
|
+
|
137
|
+
def wait_cncr_proc
|
138
|
+
@thread && @thread.join
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
|
143
|
+
module ConcurrentProcess
|
144
|
+
class IPCDuplexChannel
|
145
|
+
def initialize
|
146
|
+
@p_pid = Process.pid
|
147
|
+
@p2c = IO.pipe('ASCII-8BIT', 'ASCII-8BIT')
|
148
|
+
@c2p = IO.pipe('ASCII-8BIT', 'ASCII-8BIT')
|
149
|
+
end
|
150
|
+
|
151
|
+
def choose_io
|
152
|
+
w_pipe, r_pipe = @p_pid == Process.pid ? [@p2c, @c2p] : [@c2p, @p2c]
|
153
|
+
@wio, @rio = w_pipe[1], r_pipe[0]
|
154
|
+
[w_pipe[0], r_pipe[1]].map(&:close)
|
155
|
+
end
|
156
|
+
|
157
|
+
def send(obj)
|
158
|
+
begin
|
159
|
+
data = Marshal.dump(obj)
|
160
|
+
@wio.write([data.size].pack("I"))
|
161
|
+
@wio.write(data)
|
162
|
+
rescue Errno::EPIPE
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def recv
|
167
|
+
size = @rio.read(4).unpack("I")[0]
|
168
|
+
Marshal.load(@rio.read(size))
|
169
|
+
end
|
170
|
+
|
171
|
+
def close
|
172
|
+
[@wio, @rio].map(&:close)
|
173
|
+
end
|
174
|
+
|
175
|
+
end
|
176
|
+
|
177
|
+
def cncr_block
|
178
|
+
@ipc_channel = IPCDuplexChannel.new
|
179
|
+
@c_pid = fork do
|
180
|
+
@ipc_channel.choose_io
|
181
|
+
yield_base_block
|
182
|
+
@ipc_channel.send(:worker_loop_finished)
|
183
|
+
end
|
184
|
+
@ipc_channel.choose_io
|
185
|
+
|
186
|
+
@thread = Thread.new do
|
187
|
+
loop do
|
188
|
+
result = @ipc_channel.recv
|
189
|
+
break if result == :worker_loop_finished
|
190
|
+
call_callbacks(result)
|
191
|
+
@req_queue.pop
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
def send_req(args)
|
197
|
+
#called from main process only
|
198
|
+
@req_queue.push(args)
|
199
|
+
@ipc_channel.send(args)
|
200
|
+
end
|
201
|
+
|
202
|
+
def receive_req
|
203
|
+
#called from worker process only
|
204
|
+
@ipc_channel.recv
|
205
|
+
end
|
206
|
+
|
207
|
+
def send_res(args)
|
208
|
+
#called from worker process only
|
209
|
+
@ipc_channel.send(args)
|
210
|
+
end
|
211
|
+
|
212
|
+
def wait_cncr_proc
|
213
|
+
begin
|
214
|
+
Process.waitpid(@c_pid)
|
215
|
+
rescue Errno::ECHILD
|
216
|
+
end
|
217
|
+
@thread && @thread.join
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
class WorkerPool < Array
|
224
|
+
def initialize(*args, **options, &work_block)
|
225
|
+
@args = args
|
226
|
+
|
227
|
+
@options = options
|
228
|
+
@max_num = @options[ :pool_max ] || 8
|
229
|
+
@set_blocks = []
|
230
|
+
if work_block
|
231
|
+
@set_blocks.push([:work_block, work_block])
|
232
|
+
end
|
233
|
+
|
234
|
+
@ready_queue = Queue.new
|
235
|
+
|
236
|
+
@callbacks = []
|
237
|
+
@callback_queue = Queue.new
|
238
|
+
@callback_thread = Thread.new do
|
239
|
+
finished_count = 0
|
240
|
+
loop do
|
241
|
+
break if (result = @callback_queue.pop).empty?
|
242
|
+
@callbacks.each do |callback|
|
243
|
+
callback.call(result[0])
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
def add_callback(&callback)
|
250
|
+
raise "block is nil" unless callback
|
251
|
+
@callbacks.push(callback)
|
252
|
+
end
|
253
|
+
|
254
|
+
|
255
|
+
def deploy_worker
|
256
|
+
w = Worker.new(*@args, type: @options[:type], &@work_block)
|
257
|
+
w.add_callback do |arg|
|
258
|
+
@callback_queue.push([arg])
|
259
|
+
@ready_queue.push(w)
|
260
|
+
end
|
261
|
+
@set_blocks.each do |symbol, block|
|
262
|
+
w.set_block(symbol, &block)
|
263
|
+
end
|
264
|
+
w.run
|
265
|
+
push w
|
266
|
+
w
|
267
|
+
end
|
268
|
+
|
269
|
+
def set_block(symbol, &block)
|
270
|
+
@set_blocks.push([symbol, block])
|
271
|
+
end
|
272
|
+
|
273
|
+
def req(*args, &work_block)
|
274
|
+
if self.size < @max_num && select{ |w| w.req_queue.size == 0 }.empty?
|
275
|
+
w = deploy_worker
|
276
|
+
w.req_queue_max.times do
|
277
|
+
@ready_queue.push(w)
|
278
|
+
end
|
279
|
+
end
|
280
|
+
w = @ready_queue.pop
|
281
|
+
w.req(*args, &work_block)
|
282
|
+
end
|
283
|
+
|
284
|
+
def join
|
285
|
+
self.map(&:quit)
|
286
|
+
self.map(&:join)
|
287
|
+
@callback_queue.push([])
|
288
|
+
@callback_thread.join
|
289
|
+
end
|
290
|
+
end
|
291
|
+
|
292
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: concurrent_worker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- mur
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-05-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description: I will write later...
|
56
|
+
email:
|
57
|
+
- mur@mur.jp
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- bin/console
|
69
|
+
- bin/setup
|
70
|
+
- concurrent_worker.gemspec
|
71
|
+
- lib/concurrent_worker.rb
|
72
|
+
- lib/concurrent_worker/version.rb
|
73
|
+
homepage: https://github.com/murjp/concurrent_worker
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubygems_version: 3.0.3
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: Concurrent worker in thread/process with preparation structure.
|
96
|
+
test_files: []
|