Re: background process readling lines and responding



Alexy,

The first thing that comes to mind is that you are writing the sent
text to the pipe and then immediately calling readlines; is that
enough time for grep to process its work and write the result back to
the pipe?

Also, by "but it swallows the first line of input and doesn't
respond!", do you mean that you are not able to write anymore text
(indicating that readlines might be blocking) or just that it never
shows grep's result?

-Szymon


On Nov 9, 6:45 am, Alexy Khrabrov <delivera...@xxxxxxxxx> wrote:
Greetings -- I'm debugging a way to call a command-line executable as a
server. In normal life it behaves like grep, reading lines from stdin
and printing responses to stdout. So I'm debugging the Ruby wrapper on
grep, as the original takes a long time to load (thus the whole
exercise). Here's what I got -- but it swallows the first line of input
and doesn't respond! Why?

#!/usr/bin/env ruby

require 'optparse'
require 'ostruct'

class Server

def initialize(cmd_line)
puts "starting server with the following command line:"
puts cmd_line
@pipe = IO.popen cmd_line, "r+"
end

def process(sent)
return nil if sent.nil? or sent =~ /^\s+$/

puts "sending: [#{sent}]"
@pipe.write(sent)

@pipe.readlines
end
end # class

def main
o = OpenStruct.new
files = OptionParser.new do |opt|
opt.on("-e", "--exe EXE", "server executable basename") { |val|
o.exe = val }
end.parse(*ARGV)

ps = Server.new(o.exe)

STDIN.each_line do |line|
out = ps.process(line)
puts out
end
end

main if $0 == __FILE__

-- Here's what happens when I try to use it:

$ aha-grep-server.rb -e "grep abra"
starting server with the following command line:
grep abra
abracadabra
sending: [abracadabra
]

-- then it just hangs there, @pipe.readlines returning nothing. What's
the right IO semantics here?

Cheers,
Alexy
--
Posted viahttp://www.ruby-forum.com/.


.



Relevant Pages

  • asynchronously writing to a file, a cheap way?
    ... performance when the processing server is running. ... end of the pipe doesn't need to be running. ... remove "nospam" change community. ... I you already have a thread pool or IOCP that you can use t handle the ...
    (microsoft.public.win32.programmer.kernel)
  • Re: asynchronously writing to a file, a cheap way?
    ... your typical asynchronous route for writing to a file, ... performance when the processing server is running. ... end of the pipe doesn't need to be running. ... I you already have a thread pool or IOCP that you can use t handle the ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Service: Datenaustausch zwischen Service und Desktop zulassen
    ... einfach ist als Pipe). ... Das hoert sich fuer mich so an als redest Du einer Loesung das Wort ... Empfaenger, der so tut als ob er der legitime named Pipe Server sei, ... named-Pipe-Server-End den Service impersonated und somit seine eigenen ...
    (microsoft.public.de.vc)
  • Re: CloseHandle() hangs on Named Pipe
    ... pipe server? ... But that is why you have specific socket API functions vs specific pipe functions where the handling are not 100% the same. ... Looking at some our old asynchronous piping client/server code, we don't even use ConnectNamedPipe(). ... Use a StopServer() "Client connection" to wake up the server and stop it. ...
    (microsoft.public.win32.programmer.kernel)
  • Multithreaded pipe server, deadlock problem
    ... I'm working on a multithreaded pipe server. ... The primary thread create name pipes and if a client is connected, it then create a thread and let the thread procedure "InstanceThread" process the clients. ... // Marker 0 ...
    (microsoft.public.win32.programmer.kernel)