librelist archives

« back to archive

POSIX_MQ and EventMachine integration

POSIX_MQ and EventMachine integration

From:
Iñaki Baz Castillo
Date:
2010-12-21 @ 11:00
Hi, as POSIX_MQ provides #to_io method (IO selectable) it's possible
to integrate it within EventMachine. I paste a working code:

--------------------
require "posix_mq"
require "eventmachine"

module PosixMQ_EM

  def initialize
    @data = ""
  end

  def notify_readable
    begin
      MQ.receive @data
      receive_message @data
    rescue Errno::EAGAIN
      $stderr.puts "ERROR: attemp to read from an empty queue"
    end
  end

  def receive_message(msg)
    puts "INFO: received msg: '#{msg}'"
  end

end


EM.run do
  MQ = POSIX_MQ.new "/some_mq, IO::RDONLY | IO::NONBLOCK
  EM.watch(MQ.to_io, PosixMQ_EM) do |conn|
    conn.notify_readable = true
  end
end
--------------------


It seems to work correctly. Just a question: does the usage of #to_io
decrease performance? any suggestion to improve the above code?

Thanks a lot.


-- 
Iñaki Baz Castillo
<ibc@aliax.net>

Re: [ruby.posix.mq] POSIX_MQ and EventMachine integration

From:
Eric Wong
Date:
2010-12-22 @ 19:36
Iñaki Baz Castillo <ibc@aliax.net> wrote:
> Hi, as POSIX_MQ provides #to_io method (IO selectable) it's possible
> to integrate it within EventMachine. I paste a working code:
> 
> --------------------
> require "posix_mq"
> require "eventmachine"
> 
> module PosixMQ_EM
> 
>   def initialize
>     @data = ""
>   end

I *think* you can make initialize take arguments and not rely on a
global "MQ" constant.

    def initialize(mq)
      @mq = mq
      @data = ""
    end

>   def notify_readable
>     begin
>       MQ.receive @data
>       receive_message @data
>     rescue Errno::EAGAIN
>       $stderr.puts "ERROR: attemp to read from an empty queue"

You'll have a lot of spurious wakeups if you have multiple reader
processes and it'll just be noise to print the above message.

>     end
>   end
> 
>   def receive_message(msg)
>     puts "INFO: received msg: '#{msg}'"
>   end
> 
> end
> 
> 
> EM.run do
>   MQ = POSIX_MQ.new "/some_mq, IO::RDONLY | IO::NONBLOCK
>   EM.watch(MQ.to_io, PosixMQ_EM) do |conn|
>     conn.notify_readable = true
>   end
> end

Maybe this works instead (totally untested):

  mq = POSIX_MQ.new "/some_mq, IO::RDONLY | IO::NONBLOCK
  EM.watch(mq.to_io, PosixMQ_EM, mq) do |conn|

> It seems to work correctly. Just a question: does the usage of #to_io
> decrease performance? any suggestion to improve the above code?

to_io shouldn't hurt performance, EM should know what to do with it as
efficiently as it can.  Ruby itself is much more overhead, as always,
but not enough to stop me from using it :)

-- 
Eric Wong

Re: [ruby.posix.mq] POSIX_MQ and EventMachine integration

From:
Iñaki Baz Castillo
Date:
2010-12-23 @ 00:51
2010/12/22 Eric Wong <normalperson@yhbt.net>:
>>   def initialize
>>     @data = ""
>>   end
>
> I *think* you can make initialize take arguments and not rely on a
> global "MQ" constant.

Ok, but would it have any impact on final perfomance? or is it just a
matter of good taste? :)


>    def initialize(mq)
>      @mq = mq
>      @data = ""
>    end
>
>>   def notify_readable
>>     begin
>>       MQ.receive @data
>>       receive_message @data
>>     rescue Errno::EAGAIN
>>       $stderr.puts "ERROR: attemp to read from an empty queue"
>
> You'll have a lot of spurious wakeups if you have multiple reader
> processes and it'll just be noise to print the above message.

Yes sure, I just added such log in order to test that such case indeed occurs :)


>>   MQ = POSIX_MQ.new "/some_mq, IO::RDONLY | IO::NONBLOCK
>>   EM.watch(MQ.to_io, PosixMQ_EM) do |conn|
>>     conn.notify_readable = true
>>   end
>> end
>
> Maybe this works instead (totally untested):
>
>  mq = POSIX_MQ.new "/some_mq, IO::RDONLY | IO::NONBLOCK
>  EM.watch(mq.to_io, PosixMQ_EM, mq) do |conn|

Yes, it should as the third parameter becomes the first argument of
PosixMQ_EM.initialize method.


>> It seems to work correctly. Just a question: does the usage of #to_io
>> decrease performance? any suggestion to improve the above code?
>
> to_io shouldn't hurt performance, EM should know what to do with it as
> efficiently as it can.  Ruby itself is much more overhead, as always,
> but not enough to stop me from using it :)

Neither me ;)

Thanks a lot.

-- 
Iñaki Baz Castillo
<ibc@aliax.net>

Re: [ruby.posix.mq] POSIX_MQ and EventMachine integration

From:
Eric Wong
Date:
2010-12-23 @ 03:52
Iñaki Baz Castillo <ibc@aliax.net> wrote:
> 2010/12/22 Eric Wong <normalperson@yhbt.net>:
> >>   def initialize
> >>     @data = ""
> >>   end
> >
> > I *think* you can make initialize take arguments and not rely on a
> > global "MQ" constant.
> 
> Ok, but would it have any impact on final perfomance? or is it just a
> matter of good taste? :)

Taste :)

-- 
Eric Wong

Re: [ruby.posix.mq] POSIX_MQ and EventMachine integration

From:
Iñaki Baz Castillo
Date:
2010-12-23 @ 15:35
2010/12/23 Eric Wong <normalperson@yhbt.net>:
>> Ok, but would it have any impact on final perfomance? or is it just a
>> matter of good taste? :)
>
> Taste :)

Taste is important, at least in Ruby ;)

-- 
Iñaki Baz Castillo
<ibc@aliax.net>