Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zackees/capturing_process
A subprocess type that streams out stdout/stderr easily
https://github.com/zackees/capturing_process
python stderr stdout subprocess
Last synced: 3 months ago
JSON representation
A subprocess type that streams out stdout/stderr easily
- Host: GitHub
- URL: https://github.com/zackees/capturing_process
- Owner: zackees
- License: mit
- Created: 2021-05-07T16:50:02.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-03-25T23:24:30.000Z (8 months ago)
- Last Synced: 2024-07-31T06:48:56.205Z (4 months ago)
- Topics: python, stderr, stdout, subprocess
- Language: Python
- Homepage:
- Size: 35.2 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Finally, a subprocess type that streams out stdout/stderr easily
[![Win_Tests](https://github.com/zackees/capturing_process/actions/workflows/push_win.yml/badge.svg)](https://github.com/zackees/capturing_process/actions/workflows/push_win.yml)
[![Ubuntu_Tests](https://github.com/zackees/capturing_process/actions/workflows/push_ubuntu.yml/badge.svg)](https://github.com/zackees/capturing_process/actions/workflows/push_ubuntu.yml)
[![MacOS_Tests](https://github.com/zackees/capturing_process/actions/workflows/push_macos.yml/badge.svg)](https://github.com/zackees/capturing_process/actions/workflows/push_macos.yml)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:
```python
from capturing_process import CapturingProcessout_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:
```python
import logging
class MyStream:
def __init__(self) -> None:
passdef 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
```python
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:
* https://pypi.org/project/capturing-process/
* https://github.com/zackees/capturing_process# 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.