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 content; if you're not, just call it synchronously.
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())
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 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
File details
Details for the file tee_subprocess-1.0.0.post0.tar.gz
.
File metadata
- Download URL: tee_subprocess-1.0.0.post0.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.10.3 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b0358226a67f5a1e93c2f9d8e79f2ebcc325451a20e294e44537f7620b70d0f7 |
|
MD5 | 1a669bdb298aca0de48eea82813b276d |
|
BLAKE2b-256 | a2ec1a80f3cb0f878fb6cb713ad4d39decb9d62adc939e2ad7f8dc13c158e315 |
File details
Details for the file tee_subprocess-1.0.0.post0-py3-none-any.whl
.
File metadata
- Download URL: tee_subprocess-1.0.0.post0-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.10.3 CPython/3.11.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3f18846dab79f1e2d28346edf3d613c7bf264d5be0de450c0dd5f84e1db9f42a |
|
MD5 | b73af52cb709cff72a2a6e32fdbea538 |
|
BLAKE2b-256 | a26ffb4629bb83a25062f8a010b2e073ae902b50711d0a30a70798d289a515ab |