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

Paul Moore p.f.moore at gmail.com
Thu Aug 2 17:45:33 EDT 2018


On Thu, 2 Aug 2018 at 20:58, <cseberino at gmail.com> wrote:
> > Sorry, but there's no "simple" answer here for you (although you may
> > well be able to get something that works well enough for your specific
> > needs - but it's not obvious from your snippet of code what you're
> > trying to achieve).
>
> To send and receive text from a subprocess..even when there are exceptions.

For an arbitrary program as the subprocess? You need some sort of
threading in your process, or maybe some sort of async processing with
non-blocking reads/writes, as you cannot assume any particular
interleaving of input and output on the part of the child. If you
control the child process, you can implement a custom protocol to do
the communication, and avoid many of the problems that way.

If you're trying to "wrap" something like the Python interpreter, it's
certainly possible, but it's tricky to get all the edge cases right (I
know, I've tried!) and you probably need to run it with the -u flag
(or with PYTHONUNBUFFERED set) to avoid IO buffers making your life
miserable ;-) Or, as you say you're on Unix, you may be able to use
ptys to do this - I gather that's what they are designed for, but as a
Windows programmer myself, I know very little about them.

Paul



More information about the Python-list mailing list