celluloid / celluloid-io

UNMAINTAINED: See celluloid/celluloid#779 - Evented sockets for Celluloid actors
https://celluloid.io
MIT License
879 stars 93 forks source link

Async socket pairs #115

Closed maasha closed 9 years ago

maasha commented 9 years ago

Is is possible to use Celluloid::IO to create async socket pairs like the below example?

require 'socket'

child_socket, parent_socket = Socket.pair(:UNIX, :DGRAM, 0)
maxlen = 1000

fork do
  parent_socket.close

  10.times do
    instruction = child_socket.recv(maxlen)
    child_socket.send("#{instruction} accomplished!", 0)
  end 
end 
child_socket.close

5.times do
  parent_socket.send("Heavy lifting", 0)
end 
5.times do
  parent_socket.send("Feather lifting", 0)
end 

10.times do
  $stdout.puts parent_socket.recv(maxlen)
end 
tarcieri commented 9 years ago

Please use the mailing list for questions. The issue tracker is for reporting issues.

1) Don't use fork with Celluloid. MRI manages memory dangerously when forking multithreaded processes and it will lead to crashes 2) If you replace all of this code with Celluloid::IO actors and sockets, I think it will do what you're expecting. I don't have time to write the code for you, though.