class Async::Container::Channel

Provides a basic multi-thread/multi-process uni-directional communication channel.

Attributes

in[R]

The input end of the pipe. @attribute [IO]

out[R]

The output end of the pipe. @attribute [IO]

Public Class Methods

new() click to toggle source

Initialize the channel using a pipe.

# File lib/async/container/channel.rb, line 30
def initialize
        @in, @out = ::IO.pipe
end

Public Instance Methods

close() click to toggle source

Close both ends of the pipe.

# File lib/async/container/channel.rb, line 53
def close
        close_read
        close_write
end
close_read() click to toggle source

Close the input end of the pipe.

# File lib/async/container/channel.rb, line 43
def close_read
        @in.close
end
close_write() click to toggle source

Close the output end of the pipe.

# File lib/async/container/channel.rb, line 48
def close_write
        @out.close
end
receive() click to toggle source

Receive an object from the pipe. Internally, prefers to receive newline formatted JSON, otherwise returns a hash table with a single key `:line` which contains the line of data that could not be parsed as JSON. @returns [Hash]

# File lib/async/container/channel.rb, line 61
def receive
        if data = @in.gets
                begin
                        return JSON.parse(data, symbolize_names: true)
                rescue
                        return {line: data}
                end
        end
end