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:

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

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

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()

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.9.tar.gz (6.0 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.9-py2.py3-none-any.whl (6.2 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: capturing_process-1.0.9.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for capturing_process-1.0.9.tar.gz
Algorithm Hash digest
SHA256 ef9b750c9abe07e83153b991ec6bbd596d5e0e5f0ff5086004b6be07d5a58156
MD5 1748da78abf129ee0ad21b38179d0ef4
BLAKE2b-256 ea97c439d89e0a2f7ff747fa8124200970495a6ee47deffa7f58d2e676882955

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for capturing_process-1.0.9-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 fff87de9035c1d11e4224660e6040d2ee6aa0bd82c15425165e51ec40e1c832c
MD5 b164637ce0b0d2247f4de1c0172e067b
BLAKE2b-256 7214f6be6fca91f33c3e11a66e90d5ea5ed5ff6fab51211e142a7e4feac22adb

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