concurrent_worker 0.1.0 → 0.1.1

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
  SHA256:
3
- metadata.gz: 678beb9ed4ddb2d3c4595a4b286e6ebd195a4c5f645460619f5725af3d3064a7
4
- data.tar.gz: 6877a4d045f8dc820175bed7ada367a553ac4be85db3390903b7fbb50291ab53
3
+ metadata.gz: f99d3718f404a7e77a8b8184db783b4dc94826480e67ff32411e07fe248d69a2
4
+ data.tar.gz: 2f568f170d9bb93aef611ec3d85170dba00077f0fbd9232ff75d105ddef640c3
5
5
  SHA512:
6
- metadata.gz: 7215dd0d8019ee6e20ff3416ec9efff7f5ead7c4144b92b8cf45e43234c96de8189651d23db08a3384d1e55676c0ad7e5216e5da611eb7d1456d568ad0129a8c
7
- data.tar.gz: b62ec465175412dede959aa285b4610b43b20b7e843d107f893318576b06decef8f41ff9ec57dd76be6189f7bf612b758563e13e629447443a51fb24c5df6d9a
6
+ metadata.gz: 5258be0aefc96435579d2444aca60721ef1839b5955f8a52b921814bf8ebd95717b0a3022edc52680e4558e61fee5fd69354e1d2ce71f20dc29793ede29e94b6
7
+ data.tar.gz: 7c20fcd0be6e27af4582fde0802adccffb8636cae1607ce9fd8bfb19bc17222e30ed8922838e494ebe3bed0ac885b7b27f414cb9e02ed45b6cee5f6cd403ed02
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ concurrent_worker (0.1.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.11.3)
10
+ rake (10.5.0)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ bundler (~> 2.0)
17
+ concurrent_worker!
18
+ minitest (~> 5.0)
19
+ rake (~> 10.0)
20
+
21
+ BUNDLED WITH
22
+ 2.0.1
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # ConcurrentWorker
2
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
3
 
5
- TODO: Delete this and the text above, and describe your gem
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,77 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ ### Worker
24
+
25
+ You can define worker block executed in other thread, and send requests to the worker.
26
+
27
+ ```ruby
28
+ require 'concurrent_worker'
29
+ Thread.abort_on_exception = true
30
+
31
+ # define a work block.
32
+ logger = ConcurrentWorker::Worker.new do |args|
33
+ printf(*args)
34
+ $stdout.flush
35
+ nil
36
+ end
37
+
38
+ ...
39
+ # call work block asynchronously with 'req' method.
40
+ logger.req("thread%d n=%d\n",0, 1)
41
+
42
+ logger.join
43
+ ```
44
+
45
+ If you need some preparation for the worker block, you can define 'base block':
46
+
47
+ ```ruby
48
+ logger = ConcurrentWorker::Worker.new do |args|
49
+ # share object with 'base block' by instance variable(@xxx).
50
+ printf(@file, *args)
51
+ @file.flush
52
+ nil
53
+ end
54
+
55
+ # define 'base block' for some preparation of 'work block'.
56
+ logger.set_block(:base_block) do
57
+ open( "log.txt" , "w" ) do |file|
58
+ @file = file
59
+ # 'yield_loop_block' must be called in 'base block'.
60
+ # 'work block' will be called in this call.
61
+ yield_loop_block
62
+ end
63
+ end
64
+ ...
65
+ ```
66
+
67
+ The 'work blick' and 'base block' are executed in a worker's instance scope, in a same thread, so that they can share object with the worker's instance variable.
68
+
69
+ ### WorkerPool
70
+ You can exec work block in some process concurrently.
71
+
72
+ ```ruby
73
+ #define a pool of 8 workers , executed in other process.
74
+ pw = ConcurrentWorker::WorkerPool.new(type: :process, pool_max: 8) do
75
+ |n|
76
+ [n , n.times.inject(:+)]
77
+ end
78
+
79
+ # you can receive the result of work block with callback block.
80
+ pw.add_callback do |n, result|
81
+ logger.log( "n=%d,result=%d", n, result)
82
+ end
83
+
84
+ (10000000..10000200).each do |n|
85
+ pw.req(n)
86
+ end
87
+
88
+ pw.join
89
+
90
+ ```
91
+
92
+ Worker uses `Marshal::dump/load` to transport ruby object to other process. So, request arguments and result objects must be able to be Marshal dumped.
93
+
26
94
 
27
95
  ## Development
28
96
 
@@ -32,7 +100,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
100
 
33
101
  ## Contributing
34
102
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/concurrent_worker.
103
+ Bug reports and pull requests are welcome on GitHub at https://github.com/murjp/concurrent_worker.
36
104
 
37
105
  ## License
38
106
 
data/README.md.backup ADDED
@@ -0,0 +1,107 @@
1
+ # ConcurrentWorker
2
+
3
+
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'concurrent_worker'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install concurrent_worker
20
+
21
+ ## Usage
22
+
23
+ ### Worker
24
+
25
+ You can define worker block executed in other thread, and send request for the worker.
26
+
27
+ ```ruby
28
+ require 'concurrent_worker'
29
+ Thread.abort_on_exception = true
30
+
31
+ # define a work block.
32
+ logger = ConcurrentWorker::Worker.new do |args|
33
+ printf(*args)
34
+ $stdout.flush
35
+ nil
36
+ end
37
+
38
+ ...
39
+ # call work block asynchronously with 'req' method.
40
+ logger.req("thread%d n=%d\n",0, 1)
41
+
42
+ logger.join
43
+ ```
44
+
45
+ If you want to write log in a file, you can define 'base block':
46
+
47
+ ```ruby
48
+ logger = ConcurrentWorker::Worker.new do |args|
49
+ # share object with 'base block' by instance variable(@xxx).
50
+ printf(@file, *args)
51
+ @file.flush
52
+ nil
53
+ end
54
+
55
+ # define 'base block' for some preparation of 'work block'.
56
+ logger.set_block(:base_block) do
57
+ open( "log.txt" , "w" ) do |file|
58
+ @file = file
59
+ # 'yield_loop_block' must be called in 'base block'.
60
+ # 'work block' will be called in this call.
61
+ yield_loop_block
62
+ end
63
+ end
64
+ ...
65
+ ```
66
+
67
+ The 'work blick' and 'base block' are executed in a worker's instance scope, in a same thread, so that they can share object with the worker's instance variable.
68
+
69
+ ### WorkerPool
70
+ You can exec work block in some process concurrently.
71
+
72
+ ```ruby
73
+ #define a pool of 8 workers , executed in other process.
74
+ pw = ConcurrentWorker::WorkerPool.new(type: :process, pool_max: 8) do
75
+ |n|
76
+ [n , n.times.inject(:+)]
77
+ end
78
+
79
+ # you can receive the result of work block with callback block.
80
+ pw.add_callback do |n, result|
81
+ logger.log( "n=%d,result=%d", n, result)
82
+ end
83
+
84
+ (10000000..10000200).each do |n|
85
+ pw.req(n)
86
+ end
87
+
88
+ pw.join
89
+
90
+ ```
91
+
92
+ Worker uses `Marshal::dump/load` to transport ruby object to other process. So, request arguments and result objects must be able to be Marshal dumped.
93
+
94
+
95
+ ## Development
96
+
97
+ 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.
98
+
99
+ 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).
100
+
101
+ ## Contributing
102
+
103
+ Bug reports and pull requests are welcome on GitHub at https://github.com/murjp/concurrent_worker.
104
+
105
+ ## License
106
+
107
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -1,3 +1,3 @@
1
1
  module ConcurrentWorker
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: concurrent_worker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - mur
@@ -62,8 +62,10 @@ files:
62
62
  - ".gitignore"
63
63
  - ".travis.yml"
64
64
  - Gemfile
65
+ - Gemfile.lock
65
66
  - LICENSE.txt
66
67
  - README.md
68
+ - README.md.backup
67
69
  - Rakefile
68
70
  - bin/console
69
71
  - bin/setup