Skip to main content

A zero-dependence async subprocess that keeps on getting stdout and stderr.

Project description

Build status Maintainability Test Coverage Tag pip install aiosubprocess PyPI version This project uses calendar-based versioning scheme Downloads This project uses the "black" style formatter for Python code

aiosubprocess

A zero-dependence async subprocess that keeps on getting stdout and stderr.

How to use

Example 1: Hello World

A classic Hello World. It prints Hello World! in the shell and redirects the stdout to print().

import asyncio
from aiosubprocess import Process

asyncio.get_event_loop().run_until_complete(
    Process("echo Hello World!", stdout=print).shell()
)
$> python ex1_minimal.py
[AIO Subprocess-0] Hello World!

Process finished with exit code 0

Example 2: Two Processes

One process writes to a file, and a second process logs the content of the file in real time.

import asyncio
from aiosubprocess import Process

loop = asyncio.get_event_loop()
reader = Process(
    """for i in {1..5}
    do
       echo "Hello $i World" > tempfile.log
       sleep 1
    done""",
    loop=loop,
    name="Writer",
)
writer = Process(
    "timeout --foreground 10s tail -f tempfile.log",
    loop=loop,
    name="Reader",
    expected_returncode=124,  # Because timeout is expected
)
awaitable_reader = reader.shell()
awaitable_writer = writer.shell()
gathered = asyncio.gather(awaitable_reader, awaitable_writer, loop=loop)
asyncio.get_event_loop().run_until_complete(gathered)
assert gathered.result() == [True, True]

Which does exactly this:

Example animation

Why?

There are many scenario where we need to keep an eye on a subprocess output. If we want to do so in realtime (and redirect it to logs or a GUI), the boilerplate is tedious.

The other solution is to wait for the subprocess to exit and read the stdout/stderr afterwards.

This library implements this boilerplate, so you don't have to.

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

aiosubprocess-2021.5.2.tar.gz (924.0 kB view details)

Uploaded Source

Built Distribution

aiosubprocess-2021.5.2-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file aiosubprocess-2021.5.2.tar.gz.

File metadata

  • Download URL: aiosubprocess-2021.5.2.tar.gz
  • Upload date:
  • Size: 924.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.25.1

File hashes

Hashes for aiosubprocess-2021.5.2.tar.gz
Algorithm Hash digest
SHA256 45a6f98ed6277571a3d9e9044ba50f2f4c500b7ca6159b8c41400240e6d4c5db
MD5 9b281035d865bc066d1225921df7e58a
BLAKE2b-256 289cf90154f332384374de6d200fd45045ae727ceb54e998595ff3532373431d

See more details on using hashes here.

File details

Details for the file aiosubprocess-2021.5.2-py3-none-any.whl.

File metadata

File hashes

Hashes for aiosubprocess-2021.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ecddf29a231790e51e864b27176b85cf5b81ebbe9f96f6546164bd5629a9c23f
MD5 9aa20be010c50282c6ef87bb6958675b
BLAKE2b-256 de437b08daaf48c9820a6fcd87381f4e9530d294bc39f4a562b6978029d326c3

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