Class: Async::Container::Channel
- Inherits:
-
Object
- Object
- Async::Container::Channel
- Defined in:
- lib/async/container/channel.rb
Overview
Provides a basic multi-thread/multi-process uni-directional communication channel.
Instance Attribute Summary collapse
-
#in ⇒ Object
The input end of the pipe.
-
#out ⇒ Object
The output end of the pipe.
Instance Method Summary collapse
-
#close ⇒ Object
Close both ends of the pipe.
-
#close_read ⇒ Object
Close the input end of the pipe.
-
#close_write ⇒ Object
Close the output end of the pipe.
-
#initialize ⇒ Channel
constructor
Initialize the channel using a pipe.
-
#receive ⇒ Object
Receive an object from the pipe.
Constructor Details
#initialize ⇒ Channel
Initialize the channel using a pipe.
30 31 32 |
# File 'lib/async/container/channel.rb', line 30 def initialize @in, @out = ::IO.pipe end |
Instance Attribute Details
#in ⇒ Object
The input end of the pipe.
36 37 38 |
# File 'lib/async/container/channel.rb', line 36 def in @in end |
#out ⇒ Object
The output end of the pipe.
40 41 42 |
# File 'lib/async/container/channel.rb', line 40 def out @out end |
Instance Method Details
#close ⇒ Object
Close both ends of the pipe.
53 54 55 56 |
# File 'lib/async/container/channel.rb', line 53 def close close_read close_write end |
#close_read ⇒ Object
Close the input end of the pipe.
43 44 45 |
# File 'lib/async/container/channel.rb', line 43 def close_read @in.close end |
#close_write ⇒ Object
Close the output end of the pipe.
48 49 50 |
# File 'lib/async/container/channel.rb', line 48 def close_write @out.close end |
#receive ⇒ Object
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.
61 62 63 64 65 66 67 68 69 |
# 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 |