Skip to content

Commit afacc74

Browse files
committed
Updated README.
1 parent 253a67a commit afacc74

File tree

1 file changed

+43
-36
lines changed

1 file changed

+43
-36
lines changed

README.md

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ or the [API documentation](http://rubydoc.info/github/jdantonio/concurrent-ruby/
4242
for more information or join our [mailing list](http://groups.google.com/group/concurrent-ruby).
4343

4444
There are many concurrency abstractions in this library. These abstractions can be broadly categorized
45-
into several general categories:
45+
into several general groups:
4646

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),
4848
[Agent](https://github.com/jdantonio/concurrent-ruby/wiki/Agent), [Channel](https://github.com/jdantonio/concurrent-ruby/wiki/Channel),
4949
[Future](https://github.com/jdantonio/concurrent-ruby/wiki/Future), [Promise](https://github.com/jdantonio/concurrent-ruby/wiki/Promise),
5050
[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.
7777

7878
```ruby
7979
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
8588
end
8689
end
8790

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
117123
```
118124

119125
## Contributors
120126

121127
* [Michele Della Torre](https://github.com/mighe)
122128
* [Chris Seaton](https://github.com/chrisseaton)
129+
* [Lucas Allan](https://github.com/lucasallan)
123130
* [Giuseppe Capizzi](https://github.com/gcapizzi)
124131
* [Brian Shirai](https://github.com/brixen)
125132
* [Chip Miller](https://github.com/chip-miller)

0 commit comments

Comments
 (0)