Endlessly expanding technology
seen from United States

seen from Singapore
seen from United States
seen from Singapore
seen from China

seen from Malaysia

seen from Malaysia
seen from United States

seen from Saudi Arabia

seen from United Kingdom
seen from United States

seen from United States
seen from United States

seen from United States
seen from Germany
seen from United States

seen from Saudi Arabia

seen from Malaysia
seen from United States
seen from United States
Endlessly expanding technology
gem install eventmachine --pre
Trying to install Eventmachine on Windows ( oh how I loath thy ) you need to install the pre-release build with the above command , and you should be fine. But don't quote me on that , thought I have installed it and it works for my needs it may not be for you.
EventMachine - How to write a better evented app
EventMachine
Reactor
使用事件迴圈重覆執行某件事. 在事件迴圈(Reactor)之中記得React行為. 運作原理 : Reactor取代Ruby的執行緒並重覆迴圈,當事件觸發時則呼叫Callback.
並且,React記得不要被(callback)Block住. 如何寫出不Block的事件?
不要有sleep(n)
不要有過長迴圈(100000.times)
不要有過長的I/O處理(mysql queries)
不要有可能無限或過長的迴圈 (while !conditions)
Fiber
輕量級的同步處理 透過Fiber,能讓EventMachine擺脫Callback地獄之中. 當然,我們也可以透過EM-Synchrony做到同樣的事情.
重要功能
Deferrables
功用 : 讓我們可以撰寫Event Driven的Code. 讓我們在任何物件成功或失敗時做Callback. 如此,我們可以spawn一個執行緒.
Channel
功用 : Pub/Sub 所有Subscribe會在Main Loop被接收.
Queue
功用 : 能跨執行緒,而所有由執行緒丟出的push都會在Main Loop被線性接收.
Iterator
功用 : 讓事件能被同步執行. 透過map或each,我們可以把多個事件丟進去. 比如
EM::Iterator.new(array,10).map(proc{|ary,iter| something_long() })
撰寫要點
Main Loop應該要越快越好
I/O相關的事應於Main Loop處理
任何可能久候的事件都應使用 EM.defer丟到Background
當有需要大量資料寫入/傳輸,會將Main Loop降速時,可以使用EM.next_tick
當Deferrable物件時,可以設定timeout跳到:failed避免過久的等待. 或用 set_deferred_status(nil)重設callback
I/O bound應使用event driven方式處理,所謂event driven前題就是沒有CPU bound
適用於EM的資源 -
mysql2
Activerecord
em-http-request
em-memcached
em-mongo
mongoid
em-jack
em-synchrony
goliath
有趣的EM Protocol -
Redis
MongoDB
CouchDB
Beanstalk
IRC
Thrift
Solr
SSH
XML Push Parser
Memcache
XMPP
DNS
延伸閱讀 - igvita : Untangling Evented Code with Ruby Fibers Dr Nic Williams : Threading versus Evented Mike Perham : Scaling Ruby with Actors, or How I Learned to Stop Worrying and Love Threads Jonathan Weiss : Advanced Eventmachine EventMachine Protocol Implementation