Skip to main content

A subprocess replacement with tee support for both synchronous and asynchronous contexts.

Project description

tee-subprocess

GitHub Workflow Status PyPI - Downloads

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tee_subprocess-1.1.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

tee_subprocess-1.1.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file tee_subprocess-1.1.0.tar.gz.

File metadata

  • Download URL: tee_subprocess-1.1.0.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.18.2 CPython/3.11.2 Linux/5.15.153.1-microsoft-standard-WSL2

File hashes

Hashes for tee_subprocess-1.1.0.tar.gz
Algorithm Hash digest
SHA256 2ee099876bfcd3cdbb2768d122fa2b5275175586e533a3479b93dbc4122397dc
MD5 bb38c31eccd92ad74c5f359eab553ec3
BLAKE2b-256 0721ae9a97532daf4fc7bbe1059d6ee8b230dd43e8d3e7e3e835315a1b3e622f

See more details on using hashes here.

File details

Details for the file tee_subprocess-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: tee_subprocess-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.18.2 CPython/3.11.2 Linux/5.15.153.1-microsoft-standard-WSL2

File hashes

Hashes for tee_subprocess-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c4265c7a0290694e55853461563bf50641e9dd1d1c8fac7e12c7b73dddee67e8
MD5 f2e3c40d612d2692bf1d1fd18690f8d9
BLAKE2b-256 23b7311178aa96e8472580d8fdb2187b6c4940909f2734878a4a05f066858555

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page