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.
Direct Known Subclasses
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(timeout: 1.0) ⇒ Channel
constructor
Initialize the channel using a pipe.
-
#receive ⇒ Object
Receive an object from the pipe.
Constructor Details
#initialize(timeout: 1.0) ⇒ Channel
Initialize the channel using a pipe.
13 14 15 16 |
# File 'lib/async/container/channel.rb', line 13 def initialize(timeout: 1.0) @in, @out = ::IO.pipe @in.timeout = timeout end |
Instance Attribute Details
#in ⇒ Object
The input end of the pipe.
20 21 22 |
# File 'lib/async/container/channel.rb', line 20 def in @in end |
#out ⇒ Object
The output end of the pipe.
24 25 26 |
# File 'lib/async/container/channel.rb', line 24 def out @out end |
Instance Method Details
#close ⇒ Object
Close both ends of the pipe.
37 38 39 40 |
# File 'lib/async/container/channel.rb', line 37 def close close_read close_write end |
#close_read ⇒ Object
Close the input end of the pipe.
27 28 29 |
# File 'lib/async/container/channel.rb', line 27 def close_read @in.close end |
#close_write ⇒ Object
Close the output end of the pipe.
32 33 34 |
# File 'lib/async/container/channel.rb', line 32 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.
45 46 47 48 49 50 51 52 |
# File 'lib/async/container/channel.rb', line 45 def receive if data = @in.gets return JSON.parse(data, symbolize_names: true) end rescue => error Console.error(self, "Error during channel receive!", error) return nil end |