socketry / cool.io

Simple evented I/O for Ruby (but please check out Celluloid::IO instead)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

EM::Deferrable replacement

imanel opened this issue · comments

I'm currently using EventMachine, but Cool.IO seems nicer to me(and I think that libev is faster that libevent), but after checking all source I can't find anything like EM::Deferrable - is it unnecessary in Cool.IO or it is just not implemented yet?

There's nothing like EM::Deferrable right now, sorry. I'll leave this ticket open as a feature request.

Then is there suggested way to write long running tasks that will not lock reactor? To make example:

require 'rubygems'
require 'cool.io'

ADDR = '127.0.0.1'
PORT = 4321

cool.io.connection :echo_server_connection do
  on_connect do
    sleep 1
    puts "#{remote_addr}:#{remote_port} connected"
  end
end

cool.io.server ADDR, PORT, :echo_server_connection
cool.io.run

I want this to allow accepting another connection before sleep is over. Any suggestions?

You can spin off long running tasks in separate threads. To unblock the event loop, you can use an AsyncWatcher:

https://github.com/tarcieri/cool.io/blob/master/lib/cool.io/async_watcher.rb

+1 for deferrable mixin.. So much of existing EM code relies on deferrables that not having it in cool.io makes transition pretty painful.

Let me revisit this...

I just moved https://github.com/dturnbull/ruby-redis to cool.io. I pulled in EM::Deferrable but I think its interface is wonky and the timer won't work so I have a new one designed that I'll be writing up this week.