I am interacting with a subprocess
and trying to detect when it is ready for my input. The problem that I am having is that the read or readline
functions rely on the '\n' delimiter at the end of the line, or an EOF to yield. Since this subprocess
never exits, there is no EOF
in the file like object. Since the keyword that I want to trigger off of does not contain that delimiter the read and readline
functions never yield. For example:
'Doing something\n'
'Doing something else\n'
'input>'
Since this process never exits, the read or read line never see an EOF
or \n
that it requires to yield.
Is there a way to read this file like object and to set a custom delimiter to input>
?
You can implement your own
readlines
function and choose the delimiter yourself:Unfortunately, due to Python default buffering policies you won't be able to grab large swaths of data if they are not provided by the process you're calling, but you can always resort to setting the
chunk_size
to1
and then read the input character by character. So, for your example, all you need to do is:And it should capture everything up to
>
from your subprocesses' STDOUT. You can also use multiple characters as separators in this version.