You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For longer-running processes that produce output continuously, I think it would be nice to have the ability to process each line of output on the fly. Additionally the ability to cleanly terminate the process as soon as some predicate is True.
Why
I'm thinking that this is useful for the following scenario that came up in my case while writing integration test code:
start monitoring some log files with tail -f foobar.log.
execute something that should result in a log message in foobar.log
if XYZ is in the standard output of the tail process --- then: we found the confirmation we were looking for and we can stop the tail command.
if XYZ was not found in the standard output --- then: we can throw an error after X amount of time.
Example
This is a demonstration of what I have in mind with subprocess:
import subprocess
def test():
# setup tail
with subprocess.Popen(
"touch /tmp/test.log && timeout 10s tail -f /tmp/test.log",
shell=True,
stdout=subprocess.PIPE,
) as proc:
# do something (in this case fake some log activity)
subprocess.check_output("echo Hello world > /tmp/test.log", shell=True)
while True:
line = proc.stdout.readline().decode("utf-8")
if not line:
break
if "Hello" in line:
# we found what we were looking for
return True
return False
print(test()) # True in this case
The text was updated successfully, but these errors were encountered:
Feature
For longer-running processes that produce output continuously, I think it would be nice to have the ability to process each line of output on the fly. Additionally the ability to cleanly terminate the process as soon as some predicate is
True
.Why
I'm thinking that this is useful for the following scenario that came up in my case while writing integration test code:
tail -f foobar.log
.foobar.log
XYZ
is in the standard output of the tail process --- then: we found the confirmation we were looking for and we can stop the tail command.XYZ
was not found in the standard output --- then: we can throw an error after X amount of time.Example
This is a demonstration of what I have in mind with
subprocess
:The text was updated successfully, but these errors were encountered: