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

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:

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:

class MyStream:
    def __init__(self, filehandle) -> None:
        self.fh = filehandle

    def write(self, data: str) -> None:
        self.fh.write(data)
        sys.stdout.write(data)

Then compose:

with open('myfile', 'w') as fd:
    out_stream = MyStream(fd)
    proc = CapturingProcess("echo hi", stdout=out_stream)
    proc.wait()  # Output will go to file and sys.stdout

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

proc = CapturingProcess("echo hi", stdout=StringIO())
proc.wait()  # stdout redirected to StringIO()

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:

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.7.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

capturing_process-1.0.7-py2.py3-none-any.whl (4.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file capturing_process-1.0.7.tar.gz.

File metadata

  • Download URL: capturing_process-1.0.7.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.1

File hashes

Hashes for capturing_process-1.0.7.tar.gz
Algorithm Hash digest
SHA256 02e43ce14e83b6593d8233300b185df682fb744c5251523ca16caa65b22c9ad4
MD5 59367bbe6e976a35384de2ee6f3d8dda
BLAKE2b-256 efeda1d554168d55641dcc8168e7c94f9e2a2a6027f175a72e07960347442b92

See more details on using hashes here.

File details

Details for the file capturing_process-1.0.7-py2.py3-none-any.whl.

File metadata

  • Download URL: capturing_process-1.0.7-py2.py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.1

File hashes

Hashes for capturing_process-1.0.7-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3f3001a428dfa57227b0d32932a120a5d126024a2ba8db0b0a711c81eec868b9
MD5 b90f62275a062bc69c98a5706a64c993
BLAKE2b-256 b45bacc8476159a1859294ae3703f976a5c442fcd1200eaee43776e84182b8dd

See more details on using hashes here.

Supported by

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