Dealing with errors in interactive subprocess running python interpreter that freeze the process
Terry Reedy
tjreedy at udel.edu
Wed Aug 1 20:51:42 EDT 2018
On 8/1/2018 4:11 PM, cseberino at gmail.com wrote:
> I can run python3 interactively in a subprocess w/ Popen but
> if I sent it text, that throws an exception, the process freezes
> instead of just printing the exception like the normal interpreter..
> why? how fix? Here is my code below.
>
> (I suspect when there is an exception, there is NO output to stdin so that
> the problem is the line below that tries to read from stdin never finishes.
> Maybe I need a different readline that can "survive" when there is no output and won't block?)
>
> ....
>
> import subprocess
>
> interpreter = subprocess.Popen(['python3', '-i'],
> stdin = subprocess.PIPE,
> stdout = subprocess.PIPE,
> stderr = subprocess.PIPE)
>
> while True:
> exp = input(">>> ").encode() + b"\n"
> interpreter.stdin.write(exp)
> interpreter.stdin.flush()
> print(interpreter.stdout.readline().strip())
subprocess is not meant for interaction through the pipes. That is why,
I have been told, IDLE uses a socket for interaction. Multiprocess is
apparently better suited for interaction without resorting to a socket.
> interpreter.stdin.close()
> interpreter.terminate()
>
--
Terry Jan Reedy
More information about the Python-list
mailing list