GithubHelp home page GithubHelp logo

zackees / capturing_process Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 36 KB

A subprocess type that streams out stdout/stderr easily

License: MIT License

Python 84.88% Shell 15.12%
python stderr stdout subprocess

capturing_process's Introduction

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.

capturing_process's People

Contributors

zackees avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.