concurrent-ruby 0.2.1 → 0.2.2
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 +7 -0
- data/LICENSE +21 -21
- data/README.md +276 -275
- data/lib/concurrent.rb +28 -28
- data/lib/concurrent/agent.rb +114 -114
- data/lib/concurrent/cached_thread_pool.rb +131 -131
- data/lib/concurrent/defer.rb +65 -65
- data/lib/concurrent/event.rb +60 -60
- data/lib/concurrent/event_machine_defer_proxy.rb +23 -23
- data/lib/concurrent/executor.rb +96 -96
- data/lib/concurrent/fixed_thread_pool.rb +99 -99
- data/lib/concurrent/functions.rb +120 -120
- data/lib/concurrent/future.rb +42 -42
- data/lib/concurrent/global_thread_pool.rb +24 -16
- data/lib/concurrent/goroutine.rb +29 -29
- data/lib/concurrent/null_thread_pool.rb +22 -22
- data/lib/concurrent/obligation.rb +67 -67
- data/lib/concurrent/promise.rb +174 -174
- data/lib/concurrent/reactor.rb +166 -166
- data/lib/concurrent/reactor/drb_async_demux.rb +83 -83
- data/lib/concurrent/reactor/tcp_sync_demux.rb +131 -131
- data/lib/concurrent/supervisor.rb +105 -105
- data/lib/concurrent/thread_pool.rb +76 -76
- data/lib/concurrent/utilities.rb +32 -32
- data/lib/concurrent/version.rb +3 -3
- data/lib/concurrent_ruby.rb +1 -1
- data/md/agent.md +123 -123
- data/md/defer.md +174 -174
- data/md/event.md +32 -32
- data/md/executor.md +187 -187
- data/md/future.md +83 -83
- data/md/goroutine.md +52 -52
- data/md/obligation.md +32 -32
- data/md/promise.md +227 -227
- data/md/thread_pool.md +224 -224
- data/spec/concurrent/agent_spec.rb +390 -386
- data/spec/concurrent/cached_thread_pool_spec.rb +125 -125
- data/spec/concurrent/defer_spec.rb +199 -195
- data/spec/concurrent/event_machine_defer_proxy_spec.rb +256 -256
- data/spec/concurrent/event_spec.rb +134 -134
- data/spec/concurrent/executor_spec.rb +200 -200
- data/spec/concurrent/fixed_thread_pool_spec.rb +83 -83
- data/spec/concurrent/functions_spec.rb +217 -217
- data/spec/concurrent/future_spec.rb +112 -108
- data/spec/concurrent/global_thread_pool_spec.rb +11 -38
- data/spec/concurrent/goroutine_spec.rb +67 -67
- data/spec/concurrent/null_thread_pool_spec.rb +57 -57
- data/spec/concurrent/obligation_shared.rb +132 -132
- data/spec/concurrent/promise_spec.rb +316 -312
- data/spec/concurrent/reactor/drb_async_demux_spec.rb +196 -196
- data/spec/concurrent/reactor/tcp_sync_demux_spec.rb +410 -410
- data/spec/concurrent/reactor_spec.rb +364 -364
- data/spec/concurrent/supervisor_spec.rb +269 -269
- data/spec/concurrent/thread_pool_shared.rb +204 -204
- data/spec/concurrent/uses_global_thread_pool_shared.rb +64 -0
- data/spec/concurrent/utilities_spec.rb +74 -74
- data/spec/spec_helper.rb +32 -32
- metadata +17 -19
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b0cd24fc7583a5575559eacbeeddc5d23fdae43f
|
4
|
+
data.tar.gz: addd7382eda8182216f5d86b0c53a75030c49439
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 658d79b611fd7f572beeb73096b80addf9fab04ee8a06f75f914a3dd780dad86038519c5338d73fb540dd62d0683dce26c5c25b5419978bef79c5e72171e9052
|
7
|
+
data.tar.gz: fa5977a3f4bb3d1195f0aa4a00fc3fea84f86ee4d9d751d2c3d1916e6e94fe09daa74885016769e41d5640d7567cdc6d47e0bbd32cbdb8bf5546041a1bf9d3aa
|
data/LICENSE
CHANGED
@@ -1,21 +1,21 @@
|
|
1
|
-
Copyright (c) Jerry D'Antonio -- released under the MIT license.
|
2
|
-
|
3
|
-
http://www.opensource.org/licenses/mit-license.php
|
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.
|
1
|
+
Copyright (c) Jerry D'Antonio -- released under the MIT license.
|
2
|
+
|
3
|
+
http://www.opensource.org/licenses/mit-license.php
|
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
CHANGED
@@ -1,275 +1,276 @@
|
|
1
|
-
# Concurrent Ruby [](https://travis-ci.org/jdantonio/concurrent-ruby?branch=master) [](https://gemnasium.com/jdantonio/concurrent-ruby)
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
* [
|
23
|
-
* [
|
24
|
-
* [
|
25
|
-
* [
|
26
|
-
* [
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
I
|
33
|
-
[
|
34
|
-
[
|
35
|
-
|
36
|
-
[
|
37
|
-
[
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
*
|
42
|
-
*
|
43
|
-
*
|
44
|
-
*
|
45
|
-
*
|
46
|
-
*
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
*
|
54
|
-
*
|
55
|
-
*
|
56
|
-
*
|
57
|
-
*
|
58
|
-
*
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
I
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
score
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
require 'concurrent
|
131
|
-
|
132
|
-
|
133
|
-
score
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
require 'concurrent
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
require 'concurrent
|
175
|
-
|
176
|
-
|
177
|
-
count
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
require 'concurrent
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
pool.post{ sleep(0.5); @expected += 100 }
|
206
|
-
pool.post{ sleep(0.5); @expected += 100 }
|
207
|
-
@expected
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
pool << proc{ sleep(0.5); @expected += 10 }
|
215
|
-
pool << proc{ sleep(0.5); @expected += 10 }
|
216
|
-
@expected
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
ec.
|
230
|
-
ec.
|
231
|
-
ec.
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
#=> '
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
>
|
261
|
-
>
|
262
|
-
>
|
263
|
-
>
|
264
|
-
>
|
265
|
-
>
|
266
|
-
>
|
267
|
-
>
|
268
|
-
>
|
269
|
-
>
|
270
|
-
>
|
271
|
-
>
|
272
|
-
>
|
273
|
-
>
|
274
|
-
>
|
275
|
-
> THE SOFTWARE
|
1
|
+
# Concurrent Ruby [](https://travis-ci.org/jdantonio/concurrent-ruby?branch=master) [](https://gemnasium.com/jdantonio/concurrent-ruby)
|
2
|
+
|
3
|
+
Modern concurrency tools including agents, futures, promises, thread pools, reactors, supervisors, and more.
|
4
|
+
Inspired by Erlang, Clojure, Go, JavaScript, actors, and classic concurrency patterns.
|
5
|
+
|
6
|
+
## Introduction
|
7
|
+
|
8
|
+
The old-school "lock and synchronize" approach to concurrency is dead. The future of concurrency
|
9
|
+
is asynchronous. Send out a bunch of independent [actors](http://en.wikipedia.org/wiki/Actor_model)
|
10
|
+
to do your bidding and process the results when you are ready. Although the idea of the concurrent
|
11
|
+
actor originated in the early 1970's it has only recently started catching on. Although there is
|
12
|
+
no one "true" actor implementation (what *exactly* is "object oriented," what *exactly* is
|
13
|
+
"concurrent programming"), many modern programming languages implement variations on the actor
|
14
|
+
theme. This library implements a few of the most interesting and useful of those variations.
|
15
|
+
|
16
|
+
Remember, *there is no silver bullet in concurrent programming.* Concurrency is hard. Very hard.
|
17
|
+
These tools will help ease the burden, but at the end of the day it is essential that you
|
18
|
+
*know what you are doing.*
|
19
|
+
|
20
|
+
The project is hosted on the following sites:
|
21
|
+
|
22
|
+
* [RubyGems project page](https://rubygems.org/gems/concurrent-ruby)
|
23
|
+
* [Source code on GitHub](https://github.com/jdantonio/concurrent-ruby)
|
24
|
+
* [YARD documentation on RubyDoc.info](http://rubydoc.info/github/jdantonio/concurrent-ruby/frames)
|
25
|
+
* [Continuous integration on Travis-CI](https://travis-ci.org/jdantonio/concurrent-ruby)
|
26
|
+
* [Dependency tracking on Gemnasium](https://gemnasium.com/jdantonio/concurrent-ruby)
|
27
|
+
* [Follow me on Twitter](https://twitter.com/jerrydantonio)
|
28
|
+
|
29
|
+
### Goals
|
30
|
+
|
31
|
+
My history with high-performance, highly-concurrent programming goes back to my days with C/C++.
|
32
|
+
I have the same scars as everyone else doing that kind of work with those languages.
|
33
|
+
I'm fascinated by modern concurrency patterns like [Actors](http://en.wikipedia.org/wiki/Actor_model),
|
34
|
+
[Agents](http://doc.akka.io/docs/akka/snapshot/java/agents.html), and
|
35
|
+
[Promises](http://promises-aplus.github.io/promises-spec/). I'm equally fascinated by languages
|
36
|
+
with strong concurrency support like [Erlang](http://www.erlang.org/doc/getting_started/conc_prog.html),
|
37
|
+
[Go](http://golang.org/doc/articles/concurrency_patterns.html), and
|
38
|
+
[Clojure](http://clojure.org/concurrent_programming). My goal is to implement those patterns in Ruby.
|
39
|
+
Specifically:
|
40
|
+
|
41
|
+
* Stay true to the spirit of the languages providing inspiration
|
42
|
+
* But implement in a way that makes sense for Ruby
|
43
|
+
* Keep the semantics as idiomatic Ruby as possible
|
44
|
+
* Support features that make sense in Ruby
|
45
|
+
* Exclude features that don't make sense in Ruby
|
46
|
+
* Keep everything small
|
47
|
+
* Be as fast as reasonably possible
|
48
|
+
|
49
|
+
## Features (and Documentation)
|
50
|
+
|
51
|
+
Several features from Erlang, Go, Clojure, Java, and JavaScript have been implemented thus far:
|
52
|
+
|
53
|
+
* Clojure inspired [Agent](https://github.com/jdantonio/concurrent-ruby/blob/master/md/agent.md)
|
54
|
+
* EventMachine inspired [Defer](https://github.com/jdantonio/concurrent-ruby/blob/master/md/defer.md)
|
55
|
+
* Clojure inspired [Future](https://github.com/jdantonio/concurrent-ruby/blob/master/md/future.md)
|
56
|
+
* Go inspired [Goroutine](https://github.com/jdantonio/concurrent-ruby/blob/master/md/goroutine.md)
|
57
|
+
* JavaScript inspired [Promise](https://github.com/jdantonio/concurrent-ruby/blob/master/md/promise.md)
|
58
|
+
* Java inspired [Thread Pools](https://github.com/jdantonio/concurrent-ruby/blob/master/md/thread_pool.md)
|
59
|
+
* Scheduled task execution with the [Executor](https://github.com/jdantonio/concurrent-ruby/blob/master/md/executor.md) service
|
60
|
+
|
61
|
+
### Is it any good?
|
62
|
+
|
63
|
+
[Yes](http://news.ycombinator.com/item?id=3067434)
|
64
|
+
|
65
|
+
### Supported Ruby versions
|
66
|
+
|
67
|
+
MRI 1.9.2, 1.9.3, and 2.0. This library is pure Ruby and has minimal gem dependencies. It should be
|
68
|
+
fully compatible with any Ruby interpreter that is 1.9.x compliant. I simply don't know enough
|
69
|
+
about JRuby, Rubinius, or the others to fully support them. I can promise good karma and
|
70
|
+
attribution on this page to anyone wishing to take responsibility for verifying compaitibility
|
71
|
+
with any Ruby other than MRI.
|
72
|
+
|
73
|
+
### Install
|
74
|
+
|
75
|
+
```shell
|
76
|
+
gem install concurrent-ruby
|
77
|
+
```
|
78
|
+
|
79
|
+
or add the following line to Gemfile:
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
gem 'concurrent-ruby'
|
83
|
+
```
|
84
|
+
|
85
|
+
and run `bundle install` from your shell.
|
86
|
+
|
87
|
+
Once you've installed the gem you must `require` it in your project:
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
require 'concurrent'
|
91
|
+
```
|
92
|
+
|
93
|
+
### Kernel Methods
|
94
|
+
|
95
|
+
Many Ruby developers consider it bad form to add function to the global (Kernel) namespace.
|
96
|
+
I don't necessarily agree. If the function acts like a low-level feature of the language
|
97
|
+
I think it is OK to add the method to the `Kernel` module. To support my personal programming
|
98
|
+
style I have chosen to implement `Kernel` methods to instance many of the objects in this
|
99
|
+
library. Out of respect for the larger Ruby community I have made these methods optional.
|
100
|
+
They are not imported with the normal `require 'concurrent'` directive. To import these
|
101
|
+
functions you must import the `concurrent/functions` library.
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
require 'concurrent'
|
105
|
+
score = agent(10) #=> NoMethodError: undefined method `agent' for main:Object
|
106
|
+
|
107
|
+
require 'concurrent/functions'
|
108
|
+
score = agent(10) #=> #<Concurrent::Agent:0x35b2b28 ...
|
109
|
+
score.value #=> 10
|
110
|
+
```
|
111
|
+
|
112
|
+
### Examples
|
113
|
+
|
114
|
+
For complete examples, see the specific documentation linked above. Below are a few examples to whet your appetite.
|
115
|
+
|
116
|
+
#### Goroutine (Go)
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
require 'concurrent'
|
120
|
+
|
121
|
+
@expected = nil
|
122
|
+
go(1, 2, 3){|a, b, c| @expected = [c, b, a] }
|
123
|
+
sleep(0.1)
|
124
|
+
@expected #=> [3, 2, 1]
|
125
|
+
```
|
126
|
+
|
127
|
+
#### Agent (Clojure)
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
require 'concurrent'
|
131
|
+
require 'concurrent/functions'
|
132
|
+
|
133
|
+
score = agent(10)
|
134
|
+
score.value #=> 10
|
135
|
+
|
136
|
+
score << proc{|current| current + 100 }
|
137
|
+
sleep(0.1)
|
138
|
+
score.value #=> 110
|
139
|
+
|
140
|
+
score << proc{|current| current * 2 }
|
141
|
+
sleep(0.1)
|
142
|
+
deref score #=> 220
|
143
|
+
|
144
|
+
score << proc{|current| current - 50 }
|
145
|
+
sleep(0.1)
|
146
|
+
score.value #=> 170
|
147
|
+
```
|
148
|
+
|
149
|
+
#### Defer (EventMachine)
|
150
|
+
|
151
|
+
```ruby
|
152
|
+
require 'concurrent'
|
153
|
+
require 'concurrent/functions'
|
154
|
+
|
155
|
+
Concurrent::Defer.new{ "Jerry D'Antonio" }.
|
156
|
+
then{|result| puts "Hello, #{result}!" }.
|
157
|
+
rescue{|ex| puts ex.message }.
|
158
|
+
go
|
159
|
+
|
160
|
+
#=> Hello, Jerry D'Antonio!
|
161
|
+
|
162
|
+
operation = proc{ raise StandardError.new('Boom!') }
|
163
|
+
callback = proc{|result| puts result }
|
164
|
+
errorback = proc{|ex| puts ex.message }
|
165
|
+
defer(operation, callback, errorback)
|
166
|
+
sleep(0.1)
|
167
|
+
|
168
|
+
#=> "Boom!"
|
169
|
+
```
|
170
|
+
|
171
|
+
#### Future (Clojure)
|
172
|
+
|
173
|
+
```ruby
|
174
|
+
require 'concurrent'
|
175
|
+
require 'concurrent/functions'
|
176
|
+
|
177
|
+
count = future{ sleep(1); 10 }
|
178
|
+
count.state #=> :pending
|
179
|
+
# do stuff...
|
180
|
+
count.value #=> 10 (after blocking)
|
181
|
+
deref count #=> 10
|
182
|
+
```
|
183
|
+
|
184
|
+
#### Promise (JavaScript)
|
185
|
+
|
186
|
+
```ruby
|
187
|
+
require 'concurrent'
|
188
|
+
require 'concurrent/functions'
|
189
|
+
|
190
|
+
p = promise("Jerry", "D'Antonio"){|a, b| "#{a} #{b}" }.
|
191
|
+
then{|result| "Hello #{result}." }.
|
192
|
+
rescue(StandardError){|ex| puts "Boom!" }.
|
193
|
+
then{|result| "#{result} Would you like to play a game?"}
|
194
|
+
sleep(1)
|
195
|
+
p.value #=> "Hello Jerry D'Antonio. Would you like to play a game?"
|
196
|
+
```
|
197
|
+
|
198
|
+
#### Thread Pools
|
199
|
+
|
200
|
+
```ruby
|
201
|
+
require 'concurrent'
|
202
|
+
|
203
|
+
pool = Concurrent::FixedThreadPool.new(10)
|
204
|
+
@expected = 0
|
205
|
+
pool.post{ sleep(0.5); @expected += 100 }
|
206
|
+
pool.post{ sleep(0.5); @expected += 100 }
|
207
|
+
pool.post{ sleep(0.5); @expected += 100 }
|
208
|
+
@expected #=> nil
|
209
|
+
sleep(1)
|
210
|
+
@expected #=> 300
|
211
|
+
|
212
|
+
pool = Concurrent::CachedThreadPool.new
|
213
|
+
@expected = 0
|
214
|
+
pool << proc{ sleep(0.5); @expected += 10 }
|
215
|
+
pool << proc{ sleep(0.5); @expected += 10 }
|
216
|
+
pool << proc{ sleep(0.5); @expected += 10 }
|
217
|
+
@expected #=> 0
|
218
|
+
sleep(1)
|
219
|
+
@expected #=> 30
|
220
|
+
```
|
221
|
+
|
222
|
+
#### Executor
|
223
|
+
|
224
|
+
```ruby
|
225
|
+
require 'concurrent'
|
226
|
+
|
227
|
+
ec = Concurrent::Executor.run('Foo'){ puts 'Boom!' }
|
228
|
+
|
229
|
+
ec.name #=> "Foo"
|
230
|
+
ec.execution_interval #=> 60 == Concurrent::Executor::EXECUTION_INTERVAL
|
231
|
+
ec.timeout_interval #=> 30 == Concurrent::Executor::TIMEOUT_INTERVAL
|
232
|
+
ec.status #=> "sleep"
|
233
|
+
|
234
|
+
# wait 60 seconds...
|
235
|
+
#=> 'Boom!'
|
236
|
+
#=> ' INFO (2013-08-02 23:20:15) Foo: execution completed successfully'
|
237
|
+
|
238
|
+
ec.kill #=> true
|
239
|
+
```
|
240
|
+
|
241
|
+
## Contributing
|
242
|
+
|
243
|
+
1. Fork it
|
244
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
245
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
246
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
247
|
+
5. Create new Pull Request
|
248
|
+
|
249
|
+
## Copyright
|
250
|
+
|
251
|
+
*Concurrent Ruby* is Copyright © 2013 [Jerry D'Antonio](https://twitter.com/jerrydantonio).
|
252
|
+
It is free software and may be redistributed under the terms specified in the LICENSE file.
|
253
|
+
|
254
|
+
## License
|
255
|
+
|
256
|
+
Released under the MIT license.
|
257
|
+
|
258
|
+
http://www.opensource.org/licenses/mit-license.php
|
259
|
+
|
260
|
+
> Permission is hereby granted, free of charge, to any person obtaining a copy
|
261
|
+
> of this software and associated documentation files (the "Software"), to deal
|
262
|
+
> in the Software without restriction, including without limitation the rights
|
263
|
+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
264
|
+
> copies of the Software, and to permit persons to whom the Software is
|
265
|
+
> furnished to do so, subject to the following conditions:
|
266
|
+
>
|
267
|
+
> The above copyright notice and this permission notice shall be included in
|
268
|
+
> all copies or substantial portions of the Software.
|
269
|
+
>
|
270
|
+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
271
|
+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
272
|
+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
273
|
+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
274
|
+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
275
|
+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
276
|
+
> THE SOFTWARE.
|