Created
March 14, 2009 23:12
-
-
Save tmm1/79224 to your computer and use it in GitHub Desktop.
slides from mwrc2009 lightning talk on EM: http://mwrc2009.confreaks.com/14-mar-2009-19-06-event-machine-aman-gupta.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'eventmachine' | |
### EM::run takes over the process | |
# puts 1 | |
# EM.run{ | |
# require 'mycode' | |
# } | |
# puts 3 # this will never happen | |
### timers and async blocks | |
# EM.run{ | |
# timer = EM::PeriodicTimer.new(0.25){ | |
# puts "#{Time.now}: hi!" | |
# } | |
# EM.add_timer(2){ timer.cancel } | |
# } | |
### long events block the reactor | |
# EM.run{ | |
# EM.add_periodic_timer(0.25){ | |
# puts "#{Time.now}: hi!" | |
# sleep 1 | |
# } | |
# } | |
### the reactor is just a while loop | |
# while true | |
# # fire timers | |
# timers.each{ |t| t.fire if t.time <= Time.now } | |
# | |
# # check network i/o | |
# select(sockets).each{ |fd| | |
# fd.connection_completed if fd.connected? | |
# fd.receive_data(fd.read) if fd.readable? | |
# fd.unbind if fd.closed? | |
# } | |
# | |
# # run threads | |
# rb_thread_schedule() if threads.any? | |
# end | |
### using EM in your webapps | |
# require 'thin' | |
# | |
# ## or, with mongrel/passenger | |
# | |
# $em = Thread.new{ EM.run{} } | |
# %w(INT TERM).each{ |sig| | |
# old=trap(sig){ EM.stop; old.call } | |
# } | |
### sending email | |
# email = EM::Protocols::SmtpClient.send( | |
# :domain => 'mysite.com', | |
# :host => 'mail.mysite.com', | |
# :from => '[email protected]', | |
# :to => '[email protected]', | |
# :content => 'hi' | |
# ) | |
# email.callback { puts 'sent the email!' } | |
# email.errback { puts 'email failed!' } | |
### http requests | |
# require 'em-http' | |
# http = EM::HttpRequest.new('http://twitter.com/statuses/update.xml') | |
# req = http.post( | |
# :head => {'Authorization' => ['user', 'pass']}, | |
# :body => 'status=hello world' | |
# ) | |
# req.errback{ puts 'failed posting to twitter' } | |
# req.callback{ |http| | |
# http.response_header.status | |
# http.response | |
# } | |
### running external processes | |
# EM.system("convert -resize 50x50 huge.png thumb.png"){ |output, status| | |
# if status.exitstatus == 0 | |
# # ... | |
# end | |
# } | |
### slow mysql queries | |
# require 'em/mysql' | |
# mysql = EventedMysql.connect(...) | |
# mysql.select('select sleep(15) as num'){ |res| | |
# res.first['num'] | |
# } | |
### message queues | |
# require 'mq' | |
# AMQP.start(:host => 'localhost'){ | |
# MQ.queue('logging').subscribe{ |logmsg| | |
# puts logmsg | |
# } | |
# } | |
### custom servers | |
# module MyServer | |
# def post_init | |
# puts "--- someone connected!" | |
# end | |
# def receive_data data | |
# puts "--- someone sent me data: #{data}" | |
# end | |
# def unbind | |
# puts "--- someone disconnected!" | |
# end | |
# end | |
# | |
# EM.start_server '127.0.0.1', 8080, MyServer | |
### other features | |
# - udp servers/clients | |
# - ssl connections and certificate handling | |
# - epoll/kqueue for c10k scalability | |
# - platform compatibility | |
# - java version for jruby | |
# - C extension for 1.8 and 1.9 | |
# - pure ruby reactor for everything else | |
### eventmachine resources | |
# Homepage:: http://rubyeventmachine.com | |
# Rubyforge Page:: http://rubyforge.org/projects/eventmachine | |
# Google Group:: http://groups.google.com/group/eventmachine | |
# Mailing List:: http://rubyforge.org/pipermail/eventmachine-talk | |
# RDoc:: http://eventmachine.rubyforge.org | |
# IRC:: #eventmachine on irc.freenode.net |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment