[Eventmachine-talk] How to keep a pipe open

Michael S. Fischer
2007-12-05T15:30:36Z

On a related note, now that the changes to make the input descriptor
blocking on a popen''ed process are in:

How can we then close the input descriptor w/o closing the output one? i.e.,
if you''ve popen''ed a process and want to send_data to it, then
close the
connection yet still receive data until the child process closes its
standard output (i.e., our input), how can we do that?

It would be great if we could do something like this, and receive the
expected result:

require ''eventmachine''

module CatHandler
        def post_init
                send_data "this is a test\n"
                close_connection_after_writing
        end

        def receive_data data
                print data
        end

        def unbind
                EventMachine::stop_event_loop
        end
end

EventMachine::run {
        EventMachine::popen("cat", CatHandler)
}

Likewise, I''ve run into some issues because EM::popen only lets the
caller
access the standard output stream of the subprocess and no others (in
particular, standard error).  Today I compensate for all of these
deficiencies by doing something kludgey like:

EventMachine::popen(%[sh -c "echo #{data} | #{command} #{args.join}
2>&1"])

Observation:  Because they both involve Connection objects, a popen is
treated the same as a network connection, despite the fact that in some very
important ways they are quite different:  popens have different attributes
(pid and exit status vs. peer IPs and ports) and have access to more than
one output stream.   In addition, unlike TCP sockets, the application
designer often may want to access the output that''s provided after the
program''s input stream has been flushed and closed.

Perhaps it''s time to subclass Connection so that we can take advantage
of
the differences while impacting existing socket code as little as possible?

Best regards,

--Michael



On Nov 27, 2007 2:49 PM, Duane Johnson <canadaduane at gmail.com> wrote:

> Hi all,
> I''ve been reading what I can from the list regarding
EventMachine::popen.
> The following two messages were helpful (for anyone else trying to use it):
>
> http://rubyforge.org/pipermail/eventmachine-talk/2007-June/000747.html
> http://rubyforge.org/pipermail/eventmachine-talk/2007-November/001227.html
>
> My question is this: How do I keep a pipe open for interactive-shell-type
> commands that "hang up"?
>
> I''m not sure I totally understand the mechanisms in place that are
causing
> this, so please feel free to chime in with obvious answers here.  As an
> example of the problem, take "TeX":
>
> EventMachine::run do
>   EventMachine::popen("tex -ipc", TexInstance)
> end
>
>
> This results in the following output (TexInstance simply forwards what it
> receives):
>
> received: This is TeX, Version 3.141592 (Web2C 7.5.6)
> received: **
> received: ! End of file on the terminal... why?
>
> This is the exact same message I receive if I start tex up and hit
> "ctrl-d" on the keyboard... so by its appearance, it seems like
my pipe is
> closing because tex is receiving an end-of-file character.
>
> Any ideas or workarounds?  Thanks!
>
> Duane Johnson
>
> _______________________________________________
> Eventmachine-talk mailing list
> Eventmachine-talk at rubyforge.org
> http://rubyforge.org/mailman/listinfo/eventmachine-talk
>
-------------- next part --------------
An HTML attachment was scrubbed...