A subprocess replacement with tee support for both synchronous and asynchronous contexts.
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tee_subprocess-1.2.0.tar.gz.
File metadata
- Download URL: tee_subprocess-1.2.0.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.20.1 CPython/3.11.2 Linux/5.15.167.4-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba1384e0ae559613a7ce9535aa9df2758285d53b743f3bbf344011f4abd24e8c
|
|
| MD5 |
1bfb91b8bf45e32865d8ba2e82af2346
|
|
| BLAKE2b-256 |
eca6b9804ab6cbc0582b3875f351751af6f1e7f9a6d9a273a0354c45378b2810
|
File details
Details for the file tee_subprocess-1.2.0-py3-none-any.whl.
File metadata
- Download URL: tee_subprocess-1.2.0-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.20.1 CPython/3.11.2 Linux/5.15.167.4-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
29867d34b0b99d5b8547ee9a95eba4c7ba408d9b98c4c4c03b0671e915876763
|
|
| MD5 |
65b54c0207ea8d8ebfbe440677f31f56
|
|
| BLAKE2b-256 |
f0d17f1fe589a7500a12fdfe93c81f8e74ff45eb7b8447177e090571a286d343
|