[Python-ideas] Running Python commands from a Shell

Dan Sommers 2QdxY4RzWzUUiLuE at potatochowder.com
Fri Feb 1 17:28:25 EST 2019


On 2/1/19 3:48 PM, Steven D'Aprano wrote:
 > On Fri, Feb 01, 2019 at 02:38:43PM -0600, Dan Sommers wrote:
 >
 >> So why not turn that around?  ksh (since way back when) and
 >> bash (since 2008, according to what I read somewhere online)
 >> have "co-processes," which allow you to run a command "in
 >> the background," and send commands and receive replies from
 >> it.  So I tried it with Python, but it didn't work:
 >>
 >>     $ coproc P3 { python3; }
 >>     $ echo 'import sys; print(sys.version)' >&${P3[1]}
 >>     $ read v <&${P3[0]}
 >>     [the read command just waits forever]
 >
 > This is another good example of the problem James was referring to in
 > the thread about clearer communication. Don't assume we all know what
 > coproc does.

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.

 >> A pile of experiments and examples from web pages later, I
 >> think it's Python and not me.  My example, with suitable
 >> changes to the literal in the echo command, works with sbcl
 >> and erl, but not python3.  If I start python3 as follows:
 >
 > What are sbcl and erl?
 >
 > I'm guessing you don't mean antimony pentachloride and a municipality in
 > Austria. Possibly Steel Bank Common Lisp and Erlang? But I'm not
 > confident about that.

Yes, the Steel Bank Common Lisp and Erlang REPLs, respectively.

 > Does your example work with more well-known interpreted languages with
 > interactive interpreters such as Ruby, Lua, Javascript (outside of the
 > browser), etc?

I don't know (I don't write software in any of those languages, and I
don't have them imstalled on my computer), but adding the "-i" flag to
my python3 command makes it work (thanks to ChrisA for suggesting "-u";
it was a short leap from there to "-i.")

Dan


More information about the Python-ideas mailing list