@@ -42,9 +42,9 @@ or the [API documentation](http://rubydoc.info/github/jdantonio/concurrent-ruby/
42
42
for more information or join our [ mailing list] ( http://groups.google.com/group/concurrent-ruby ) .
43
43
44
44
There are many concurrency abstractions in this library. These abstractions can be broadly categorized
45
- into several general categories :
45
+ into several general groups :
46
46
47
- * Asynchronous concurrency abstractions including [ Actor ] ( https://github.com/jdantonio/concurrent-ruby/wiki/Actor ) ,
47
+ * Asynchronous concurrency abstractions including [ Async ] ( https://github.com/jdantonio/concurrent-ruby/wiki/Async ) ,
48
48
[ Agent] ( https://github.com/jdantonio/concurrent-ruby/wiki/Agent ) , [ Channel] ( https://github.com/jdantonio/concurrent-ruby/wiki/Channel ) ,
49
49
[ Future] ( https://github.com/jdantonio/concurrent-ruby/wiki/Future ) , [ Promise] ( https://github.com/jdantonio/concurrent-ruby/wiki/Promise ) ,
50
50
[ ScheculedTask] ( https://github.com/jdantonio/concurrent-ruby/wiki/ScheduledTask ) ,
@@ -77,49 +77,56 @@ This one simple example shows some of the power of this gem.
77
77
78
78
``` ruby
79
79
require ' concurrent'
80
- require ' faker'
81
-
82
- class EchoActor < Concurrent ::Actor
83
- def act (* message )
84
- puts " #{ message } handled by #{ self } "
80
+ require ' thread' # for Queue
81
+ require ' open-uri' # for open(uri)
82
+
83
+ class Ticker
84
+ def get_year_end_closing (symbol , year )
85
+ uri = " http://ichart.finance.yahoo.com/table.csv?s=#{ symbol } &a=11&b=01&c=#{ year } &d=11&e=31&f=#{ year } &g=m"
86
+ data = open (uri) {|f | f.collect{|line | line.strip } }
87
+ data[1 ].split(' ,' )[4 ].to_f
85
88
end
86
89
end
87
90
88
- mailbox, pool = EchoActor .pool(5 )
89
-
90
- timer_proc = proc do
91
- mailbox.post(Faker ::Company .bs)
92
- end
93
-
94
- t1 = Concurrent ::TimerTask .new (execution_interval: rand (5 )+ 1 , & timer_proc)
95
- t2 = Concurrent ::TimerTask .new (execution_interval: rand (5 )+ 1 , & timer_proc)
96
-
97
- overlord = Concurrent ::Supervisor .new
98
-
99
- overlord.add_worker(t1)
100
- overlord.add_worker(t2)
101
- pool.each{|actor | overlord.add_worker(actor)}
102
-
103
- overlord.run!
104
-
105
- # => ["mesh proactive platforms"] handled by #<EchoActor:0x007fa5ac18bdf8>
106
- # => ["maximize sticky portals"] handled by #<EchoActor:0x007fa5ac18bdd0>
107
- # => ["morph bleeding-edge markets"] handled by #<EchoActor:0x007fa5ac18bd80>
108
- # => ["engage clicks-and-mortar interfaces"] handled by #<EchoActor:0x007fa5ac18bd58>
109
- # => ["monetize transparent infrastructures"] handled by #<EchoActor:0x007fa5ac18bd30>
110
- # => ["morph sexy e-tailers"] handled by #<EchoActor:0x007fa5ac18bdf8>
111
- # => ["exploit dot-com models"] handled by #<EchoActor:0x007fa5ac18bdd0>
112
- # => ["incentivize virtual deliverables"] handled by #<EchoActor:0x007fa5ac18bd80>
113
- # => ["enhance B2B models"] handled by #<EchoActor:0x007fa5ac18bd58>
114
- # => ["envisioneer real-time architectures"] handled by #<EchoActor:0x007fa5ac18bd30>
115
-
116
- overlord.stop
91
+ # Future
92
+ price = Concurrent ::Future .execute{ Ticker .new .get_year_end_closing(' TWTR' , 2013 ) }
93
+ price.state # => :pending
94
+ sleep (1 ) # do other stuff
95
+ price.value # => 63.65
96
+ price.state # => :fulfilled
97
+
98
+ # Promise
99
+ prices = Concurrent ::Promise .new { puts Ticker .new .get_year_end_closing(' AAPL' , 2013 ) }.
100
+ then { puts Ticker .new .get_year_end_closing(' MSFT' , 2013 ) }.
101
+ then { puts Ticker .new .get_year_end_closing(' GOOG' , 2013 ) }.
102
+ then { puts Ticker .new .get_year_end_closing(' AMZN' , 2013 ) }.execute
103
+ prices.state # => :pending
104
+ sleep (1 ) # do other stuff
105
+ # => 561.02
106
+ # => 37.41
107
+ # => 1120.71
108
+ # => 398.79
109
+
110
+ # ScheduledTask
111
+ task = Concurrent ::ScheduledTask .execute(2 ){ Ticker .new .get_year_end_closing(' INTC' , 2013 ) }
112
+ task.state # => :pending
113
+ sleep (3 ) # do other stuff
114
+ task.value # => 25.96
115
+
116
+ # Async
117
+ ticker = Ticker .new
118
+ ticker.extend (Concurrent ::Async )
119
+ hpq = ticker.async.get_year_end_closing(' HPQ' , 2013 )
120
+ ibm = ticker.await.get_year_end_closing(' IBM' , 2013 )
121
+ hpq.value # => 27.98
122
+ ibm.value # 187.57
117
123
```
118
124
119
125
## Contributors
120
126
121
127
* [ Michele Della Torre] ( https://github.com/mighe )
122
128
* [ Chris Seaton] ( https://github.com/chrisseaton )
129
+ * [ Lucas Allan] ( https://github.com/lucasallan )
123
130
* [ Giuseppe Capizzi] ( https://github.com/gcapizzi )
124
131
* [ Brian Shirai] ( https://github.com/brixen )
125
132
* [ Chip Miller] ( https://github.com/chip-miller )
0 commit comments