Skip to content

Instantly share code, notes, and snippets.

@tmm1
Created March 14, 2009 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tmm1/79224 to your computer and use it in GitHub Desktop.
Save tmm1/79224 to your computer and use it in GitHub Desktop.
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 => 'ruby@mysite.com',
# :to => 'customer@gmail.com',
# :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