tee-subprocess
A subprocess replacement with tee support for both synchronous and asynchronous contexts.
Supports Python 3.8+.
Example
Just import the run function and use it as you would use subprocess.run.
from tee_subprocess import run
process = run(["python", "--version"], tee=True, text=True, capture_output=True)
# ==> Python 3.11.2
print(process.stdout)
# ==> Python 3.11.2
Changing stdout and stderr changes the location to which the tee occurs. You can supply any of the defined options in subprocess or asyncio.subprocess (STDOUT, DEVNULL, etc), as well as a writable text or binary file object; if providing a text file object, you must specify text = True.
Async
Internally, tee_subprocess utilizes asyncio to concurrently output and capture the subprocess logs. If an event loop is already running, run will return an awaitable coroutine. Otherwise, it will call asyncio.run for you. Practically, this means you can just treat run as a coroutine if you're in an async context.
async def main():
process = await run(["python", "--version"], tee=True, text=True, capture_output=True)
# ==> Python 3.11.2
print(process.stdout)
# ==> Python 3.11.2
asyncio.run(main())
Static Typing
I do my best to provide a logical static function typing for any permitted invocation style. Mypy should complain about missing or invalid overloads if you attempt to use a combination of arguments with undefined behavior (like supplying text=True while also providing a BytesIO as stdout, or supplying a PathLike argument while shell=True).
The one fairly large exception to this is await run(...) vs run(...). For now, run returns a union between a complete process and a coroutine due to the runtime-check for an asyncio context. As a result, you'll have to cast the run(...) call to either an awaitable or a CompletedProcess depending on your specific use. The API may change in the future to avoid this problem.
Alternatives
subprocess-tee, the motivation for this library, has the same objective but fails to accommodate asynchronous applications and non-shell invocations. This library supports asynchronous contexts as well as direct, non-shell, program execution ("list-style" vs. "shell-style").
License
MIT License Copyright (c) 2023-2024 Elias Gabriel
Metadata
Release files for tee-subprocess 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tee_subprocess-1.2.0.tar.gz | 6.9 kB | Without provenance | Details for tee_subprocess-1.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tee_subprocess-1.2.0-py3-none-any.whl | Python 3 | none | any | Details for tee_subprocess-1.2.0-py3-none-any.whl |
Total release size: 13.7 kB
Release files / tee_subprocess-1.2.0.tar.gz
| Download URL | tee_subprocess-1.2.0.tar.gz |
|---|---|
| Size | 6.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ba1384e0ae559613a7ce9535aa9df2758285d53b743f3bbf344011f4abd24e8c
|
|
BLAKE2b-256 checksum How to use checksums |
eca6b9804ab6cbc0582b3875f351751af6f1e7f9a6d9a273a0354c45378b2810
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.20.1 CPython/3.11.2 Linux/5.15.167.4-microsoft-standard-WSL2
|
Release files / tee_subprocess-1.2.0-py3-none-any.whl
| Download URL | tee_subprocess-1.2.0-py3-none-any.whl |
|---|---|
| Size | 6.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
29867d34b0b99d5b8547ee9a95eba4c7ba408d9b98c4c4c03b0671e915876763
|
|
BLAKE2b-256 checksum How to use checksums |
f0d17f1fe589a7500a12fdfe93c81f8e74ff45eb7b8447177e090571a286d343
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.20.1 CPython/3.11.2 Linux/5.15.167.4-microsoft-standard-WSL2
|
Security for tee-subprocess 1.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.