Class: Async::Container::Channel

Inherits:
Object
  • Object
show all
Defined in:
lib/async/container/channel.rb

Overview

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

Direct Known Subclasses

Forked::Child, Threaded::Child

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChannel

Initialize the channel using a pipe.



13
14
15
# File 'lib/async/container/channel.rb', line 13

def initialize
	@in, @out = ::IO.pipe
end

Instance Attribute Details

#inObject

The input end of the pipe.



19
20
21
# File 'lib/async/container/channel.rb', line 19

def in
  @in
end

#outObject

The output end of the pipe.



23
24
25
# File 'lib/async/container/channel.rb', line 23

def out
  @out
end

Instance Method Details

#closeObject

Close both ends of the pipe.



36
37
38
39
# File 'lib/async/container/channel.rb', line 36

def close
	close_read
	close_write
end

#close_readObject

Close the input end of the pipe.



26
27
28
# File 'lib/async/container/channel.rb', line 26

def close_read
	@in.close
end

#close_writeObject

Close the output end of the pipe.



31
32
33
# File 'lib/async/container/channel.rb', line 31

def close_write
	@out.close
end

#receiveObject

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.



44
45
46
47
48
49
50
51
52
# File 'lib/async/container/channel.rb', line 44

def receive
	if data = @in.gets
		begin
			return JSON.parse(data, symbolize_names: true)
		rescue
			return {line: data}
		end
	end
end