* POSIX_MQ and EventMachine integration
@ 2010-12-21 11:00 Iñaki Baz Castillo
2010-12-22 19:36 ` Eric Wong
0 siblings, 1 reply; 5+ messages in thread
From: Iñaki Baz Castillo @ 2010-12-21 11:00 UTC (permalink / raw)
To: ruby.posix.mq
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>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: POSIX_MQ and EventMachine integration
2010-12-21 11:00 POSIX_MQ and EventMachine integration Iñaki Baz Castillo
@ 2010-12-22 19:36 ` Eric Wong
2010-12-23 0:51 ` Iñaki Baz Castillo
0 siblings, 1 reply; 5+ messages in thread
From: Eric Wong @ 2010-12-22 19:36 UTC (permalink / raw)
To: ruby.posix.mq
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: POSIX_MQ and EventMachine integration
2010-12-22 19:36 ` Eric Wong
@ 2010-12-23 0:51 ` Iñaki Baz Castillo
2010-12-23 3:52 ` Eric Wong
0 siblings, 1 reply; 5+ messages in thread
From: Iñaki Baz Castillo @ 2010-12-23 0:51 UTC (permalink / raw)
To: ruby.posix.mq
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>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: POSIX_MQ and EventMachine integration
2010-12-23 0:51 ` Iñaki Baz Castillo
@ 2010-12-23 3:52 ` Eric Wong
2010-12-23 15:35 ` Iñaki Baz Castillo
0 siblings, 1 reply; 5+ messages in thread
From: Eric Wong @ 2010-12-23 3:52 UTC (permalink / raw)
To: ruby.posix.mq
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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: POSIX_MQ and EventMachine integration
2010-12-23 3:52 ` Eric Wong
@ 2010-12-23 15:35 ` Iñaki Baz Castillo
0 siblings, 0 replies; 5+ messages in thread
From: Iñaki Baz Castillo @ 2010-12-23 15:35 UTC (permalink / raw)
To: ruby.posix.mq
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>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2010-12-23 15:35 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-12-21 11:00 POSIX_MQ and EventMachine integration Iñaki Baz Castillo
2010-12-22 19:36 ` Eric Wong
2010-12-23 0:51 ` Iñaki Baz Castillo
2010-12-23 3:52 ` Eric Wong
2010-12-23 15:35 ` Iñaki Baz Castillo
Code repositories for project(s) associated with this public inbox
https://yhbt.net/ruby_posix_mq.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).