Module: PPipe::Reader
Overview
PPipe requires a method of reading pipes that will not block even if the pipe is empty, is at eof, or has not been written to yet. configure_reader dynamically defines this method, which is called read_pipe
Reader is included in PPipe. It can also be included independently in any class
Instance Attribute Summary
Attributes included from Log
Instance Method Summary collapse
-
#configure_reader ⇒ Object
The best way of defining Reader#read_pipe is operating system dependent.
-
#get_line(pipe, sep = $/) ⇒ Object
Get the next line from the pipe.
-
#read_pipe(pipe) ⇒ Object
Read all the contents of the pipe.
Methods included from Log
clean_up, io=, #log, log_file, log_file=
Instance Method Details
#configure_reader ⇒ Object
The best way of defining Reader#read_pipe is operating system dependent. Work out which is the best way and then define the method read_pipe to be this method.
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 |
# File 'lib/parallelpipes.rb', line 951 def configure_reader @verbosity ||= 0 # for logging purposes pipes = [nil, IO.pipe] pipe = IO.pipe pipes[1][1].sync = true pipe[1].sync = true my_pipes = [] pid = Kernel.fork if pid pipe[1].close my_pipes.push pipe[0] pipes[1][0].close pipes[1] = pipes[1][1] # pipes[1].puts # sleep 0.002 log 'v8', 'sending pears' pipes[1].sync = true pipes[1].print('pears' + "\n") # cmd = [2].pack("S") # pipes[1].ioctl(5, cmd) # sleep 1 log 'v8', 'waiting for apples...' method = determine_read(my_pipes[0], "apples\nare\nnice\n") log 'v8', method self.class.send(:alias_method, :read_pipe, method) self.class.send(:alias_method, :get_line, method + :_get_line) my_pipes[0].close pipes[1].close return method else pipes[1][1].close my_pipes.push pipes[1][0] pipes[1] = nil pipe[0].close pipes[0] = pipe[1] pipes[0].sync = true log 'v8', pipes.inspect # sleep 0.5 log 'v8', 'waiting for pears' log 'v8', determine_read(my_pipes[0], "pears\n") log 'v8', 'sending apples' pipes[0].print("apples\nare\nnice" + "\n") pipes[0].flush exit end end |
#get_line(pipe, sep = $/) ⇒ Object
Get the next line from the pipe. Return nil if the pipe is empty or at eof. Blocks if the pipe is not empty but does not contain a complete line.
812 813 |
# File 'lib/parallelpipes.rb', line 812 def get_line(pipe, sep = $/) end |
#read_pipe(pipe) ⇒ Object
Read all the contents of the pipe. Return nil if the pipe is empty. Never, ever, ever, ever block!
807 808 |
# File 'lib/parallelpipes.rb', line 807 def read_pipe(pipe) end |