Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: handle streaming output #140

Open
rayburgemeestre opened this issue Jul 6, 2021 · 0 comments
Open

Feature request: handle streaming output #140

rayburgemeestre opened this issue Jul 6, 2021 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@rayburgemeestre
Copy link

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:

  • 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
@penguinolog penguinolog self-assigned this Jul 9, 2021
@penguinolog penguinolog added the enhancement New feature or request label Jul 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants