In the Go programming language, you can send Messages around using a construct called "Channels". http://golang.org/doc/effective_go.html#channels
I would love to use something like that in Ruby, especially for IPC.
Pseudocode of what I want:
channel = Channel.new
fork do
3.times{ channel.send("foo ") }
exit!
end
Thread.new do
3.times{ channel.send("bar ") }
end
loop do
print channel.recv
end
# ~> bar foo foo bar bar foo
Is there any construct, library or equivalent for Ruby which works like that ?
If not: What is the best way to build such an abstraction?
UPDATE: To clarify what I need from these Channels.
One use case: Some forked workers waiting for jobs. They all read from the same JobChannel and report results to the same ResultChannel.
The Channels I need
- are very fast,
- writes do not block, (message sending)
- reads do block, (message receiving)
- do not need special treatment before forking,
- lightweight and simple would be nice.
So far I played around with
- DRb, (opposite of lightweight + slow + too much magic for my little brain)
- Sockets, (UNIXSocket, TCPSocket ... Sockets seem to have many many ways of using them. I got a half-working channel on UNIXSockets. If you think sockets make sense, what subset of features should I look at?)
- Pipes. (Connecting more than 2 Processes seems to be non-trivial)
If any of those was already the perfect technology for my problem, please provide tutorials etc. which have more focused information on my requirements.