Dealing with errors in interactive subprocess running python interpreter that freeze the process

Chris Angelico rosuav at gmail.com
Wed Aug 1 16:35:01 EDT 2018


On Thu, Aug 2, 2018 at 6:11 AM,  <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())

Normally, I would treat the input and output pipes separately. One
easy (if potentially inefficient) way to do this is to create a thread
for each of the pipes coming back - stdin and stderr, in this case -
and have each one read from one pipe and display it. Then your main
thread can ignore stdout and just push info into the stdin pipe.

> interpreter.stdin.close()
> interpreter.terminate()

Side point: I would recommend joining, rather than forcibly
terminating, the subprocess. Close stdin, then wait for it to finish;
if you're paranoid, wait a limited amount of time and THEN terminate
it, but otherwise, just join() the process.

ChrisA



More information about the Python-list mailing list