On 2/1/19, Steven D'Aprano <st...@pearwood.info> wrote:
> On Fri, Feb 01, 2019 at 04:28:25PM -0600, Dan Sommers wrote:
>
>> As I indicated in what you quoted, shell co-processes allow you to run a
>> command in the background and interact with that command from your
>> shell.
>
> Okay, but what does that mean in practice? What does it require to make
> it work with Python? What is your expected input and output?

bash coproc runs a process in the background with stdin and stdout
redirected to pipes. The file descriptors for our end of the pipes are
available in an array with the given name (e.g. P3). The default array
name is COPROC.

As soon as  "pipe" is mentioned, anyone familiar with the REPL's
behavior with pipes should know that making this work will require the
-i command-line option to force interactive mode. Otherwise stdout
will be fully buffered. For example:

    $ coproc P3 { python3 -qi 2>&1; }
    [1] 16923
    $ echo 'import sys; print(sys.version)' >&${P3[1]}

    $ read -t 1 <&${P3[0]} && echo $REPLY
    >>> 3.6.7 (default, Oct 22 2018, 11:32:17)

    $ read -t 1 <&${P3[0]} && echo $REPLY
    [GCC 8.2.0]

    $ read -t 1 <&${P3[0]} && echo $REPLY
    $ echo 'sys.exit(42)' >&${P3[1]}
    $
    [1]+  Exit 42                 coproc P3 { python3 -qi 2>&1; }

> And are we supposed to know what ">&${P3[1]}" does? It looks like your
> cat walked over your keyboard.

It redirects the command's standard output (>) to the file descriptor
(&) in index 1 of the P3 array (${P3[1]}), which is our end of the
pipe that's connected to stdin of the co-process.
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to