#367 Celluloid pro
Jul 18, 2012 | 11 minutes | Tools
Celluloid puts an object-oriented spin on multi-threaded development. Here I cover the fundamental features of Celluloid in several examples.
- Download:
- source code Project Files in Zip (1.17 KB)
- mp4 Full Size H.264 Video (26 MB)
- m4v Smaller H.264 Video (12.7 MB)
- webm Full Size VP8 Video (14.2 MB)
- ogv Full Size Theora Video (25.4 MB)
Browse_code
Browse Source Code
Resources
terminal
gem install celluloid irb -r ./rocket ruby feed_counter.rb http://rss.cnn.com/rss/cnn_topstories.rss http://feeds.feedburner.com/railscasts http://stackoverflow.com/feeds irb -r ./feed_counter
irb
r = Rocket.new r.launch r.launch! r2 = Rocket.new Thread.list.size r.launch!; r2.launch! l = Launcher.new l.launch_rocket Rocket.supervise(true) f = FeedCounter.new("http://feeds.feedburner.com/railscasts") future = f.future(:count) future.value
rocket.rb
require 'celluloid' class Rocket include Celluloid def initialize(autolaunch = false) launch! if autolaunch end def launch 3.downto(1) do |n| puts "#{n}..." sleep 1 raise "Houston, we have a problem" if [true, false].sample end puts "Blast off!" end end class Launcher include Celluloid trap_exit :relaunch def launch_rocket Rocket.new_link.launch! end def relaunch(actor, reason) launch_rocket end end
feed_counter.rb
require 'rss' require 'open-uri' require 'celluloid' class FeedCounter include Celluloid def count(url) open(url) do |f| rss = RSS::Parser.parse(f.read, false) count = rss.items.size puts "#{count} in #{url}" count end end end pool = FeedCounter.pool(size: 6) futures = $*.map { |url| pool.future(:count, url) } total = futures.map(&:value).inject(:+) puts "#{total} total" if total
loading