concurrent_worker 0.1.2 → 0.1.3
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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +13 -15
- data/concurrent_worker.gemspec +3 -3
- data/lib/concurrent_worker/version.rb +1 -1
- metadata +5 -6
- data/README.md.backup +0 -107
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7800e859a8467ccbb2980cff86f3213b4d4c945f4452acc5fa964551003438b0
|
4
|
+
data.tar.gz: eecb1fac2fb3cf9769040ee7de854eeb8203e72a615539f6adc77e636b557f08
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c73661e8942dcf02aa5d713cddace082e7fc4eaf75c3d844ca74fa58d0dea93c3c893b4d41065893fb6c85d2f32c93d0f9f30dd892bdba6d04d9bfab76808b0f
|
7
|
+
data.tar.gz: 4fb57e9d3445dc0cf93836ceef3b7d52d4df669e855b18c2a498f77a3565bcc288289cea88945b404b526e147e20a92b5657084c6dc764f64d71497e9c111c2e
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# ConcurrentWorker
|
2
2
|
|
3
|
-
|
3
|
+
Concurrent worker in thread/process with preparation structure.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
|
-
Add this line to your application's Gemfile
|
7
|
+
Add this line to your application's Gemfile.
|
8
8
|
|
9
9
|
```ruby
|
10
10
|
gem 'concurrent_worker'
|
@@ -42,51 +42,49 @@ logger.req("thread%d n=%d\n",0, 1)
|
|
42
42
|
logger.join
|
43
43
|
```
|
44
44
|
|
45
|
-
If you need some preparation for the worker block, you can define 'base block'
|
45
|
+
If you need some preparation for the worker block, you can define 'base block'.
|
46
46
|
|
47
47
|
```ruby
|
48
48
|
logger = ConcurrentWorker::Worker.new do |args|
|
49
|
-
#
|
49
|
+
# worker block and base block can share object with instance variable(@xxx).
|
50
50
|
printf(@file, *args)
|
51
51
|
@file.flush
|
52
52
|
nil
|
53
53
|
end
|
54
54
|
|
55
|
-
# define
|
55
|
+
# define base block for some preparation of work block.
|
56
56
|
logger.set_block(:base_block) do
|
57
57
|
open( "log.txt" , "w" ) do |file|
|
58
58
|
@file = file
|
59
|
-
# 'yield_loop_block' must be called in
|
60
|
-
#
|
59
|
+
# 'yield_loop_block' must be called in base block.
|
60
|
+
# work block will be called in this call.
|
61
61
|
yield_loop_block
|
62
62
|
end
|
63
63
|
end
|
64
64
|
...
|
65
65
|
```
|
66
66
|
|
67
|
-
The
|
67
|
+
The work block and base block are executed in a same thread, and in a worker's instance scope so that they can share object with the worker's instance variable.
|
68
68
|
|
69
69
|
### WorkerPool
|
70
70
|
You can exec work block in some process concurrently.
|
71
71
|
|
72
72
|
```ruby
|
73
73
|
#define a pool of 8 workers , executed in other process.
|
74
|
-
|
75
|
-
|n|
|
74
|
+
wp = ConcurrentWorker::WorkerPool.new(type: :process, pool_max: 8) do |n|
|
76
75
|
[n , n.times.inject(:+)]
|
77
76
|
end
|
78
77
|
|
79
78
|
# you can receive the result of work block with callback block.
|
80
|
-
|
79
|
+
wp.add_callback do |n, result|
|
81
80
|
logger.log( "n=%d,result=%d", n, result)
|
82
81
|
end
|
83
82
|
|
84
83
|
(10000000..10000200).each do |n|
|
85
|
-
|
84
|
+
wp.req(n)
|
86
85
|
end
|
87
86
|
|
88
|
-
|
89
|
-
|
87
|
+
wp.join
|
90
88
|
```
|
91
89
|
|
92
90
|
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.
|
@@ -100,7 +98,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
100
98
|
|
101
99
|
## Contributing
|
102
100
|
|
103
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
101
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/dddogdiamond/concurrent_worker.
|
104
102
|
|
105
103
|
## License
|
106
104
|
|
data/concurrent_worker.gemspec
CHANGED
@@ -6,11 +6,11 @@ require "concurrent_worker/version"
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "concurrent_worker"
|
8
8
|
spec.version = ConcurrentWorker::VERSION
|
9
|
-
spec.authors = ["
|
10
|
-
spec.email = ["
|
9
|
+
spec.authors = ["dddogdiamond"]
|
10
|
+
spec.email = ["dddogdiamond@gmail.com"]
|
11
11
|
|
12
12
|
spec.summary = %q{Concurrent worker in thread/process with preparation structure.}
|
13
|
-
spec.description = %q{
|
13
|
+
spec.description = %q{Concurrent worker in thread/process with preparation structure.}
|
14
14
|
spec.homepage = "https://github.com/dddogdiamond/concurrent_worker"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
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.1.
|
4
|
+
version: 0.1.3
|
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-
|
11
|
+
date: 2019-05-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,9 +52,9 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '5.0'
|
55
|
-
description:
|
55
|
+
description: Concurrent worker in thread/process with preparation structure.
|
56
56
|
email:
|
57
|
-
-
|
57
|
+
- dddogdiamond@gmail.com
|
58
58
|
executables: []
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
@@ -65,7 +65,6 @@ files:
|
|
65
65
|
- Gemfile.lock
|
66
66
|
- LICENSE.txt
|
67
67
|
- README.md
|
68
|
-
- README.md.backup
|
69
68
|
- Rakefile
|
70
69
|
- bin/console
|
71
70
|
- bin/setup
|
data/README.md.backup
DELETED
@@ -1,107 +0,0 @@
|
|
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).
|