Skip to main content

Captures stderr/stdout as a stream to allow easy log monitoring of long running shell processes.",

Project description

Finally, a subprocess type that streams out stdout/stderr easily

Win_Tests Ubuntu_Tests MacOS_Tests

Capturing the stderr AND stdout from a process in python is not that easy. This class makes this capturing much easier by delegating the line capturing to seperate threads. This capture can be totally in memory or can optionally be streamed to a output stream such as a file handle.

This class will unconditionally launch a shell command and the input will always be string, not an array like what is accepted by subprocess.Popen().

Example:

Super simple example:

from capturing_process import CapturingProcess

out_stream = StringIO()
p = CapturingProcess("echo hi", stdout=out_stream)
p.wait()
self.assertIn("hi", out_stream.getvalue())
self.assertIn("hi", p.get_stdout())

For splitting the output to stdout and a file you'd write a stream class like so:

import logging
class MyStream:
    def __init__(self) -> None:
        pass

    def write(self, data: str) -> None:
        logging.info(data.rstrip('\n'))
        print(data, end="")


out_stream = MyStream()
proc = CapturingProcess("echo hi", stdout=out_stream)
proc.wait()  # Output will be captured in logging file and stdout

To silence an output stream (stdout/stderr) drop a StringIO object as an argument to the CapturingProcess like so:

If you want the entire stdout/stderr bytes

proc.get_stdout()
proc.get_stderr()

Python version: 3.6+

Because of the use of type annotations, this library is not compatible with python 2.7 However you are free to strip out these type annotations and this project should work pretty well.

Links:

Versions

  • 1.0.9: stdout/stderr threads are now forcefully killed within .1 second if they don't join.
  • 1.0.8: Fixes CapturingProcess.kill blocking if the stdout and stderr threads fail to join.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

capturing_process-1.0.13.tar.gz (6.5 kB view hashes)

Uploaded Source

Built Distribution

capturing_process-1.0.13-py2.py3-none-any.whl (6.6 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page