Communicate with a subprocess using iterables: for when data is too big to fit in memory and has to be streamed
Project description
iterable-subprocess
Python context manager to communicate with a subprocess using iterables: for when data is too big to fit in memory and has to be streamed.
Data is sent to a subprocess's standard input via an iterable, and extracted from its standard output via another iterable. This allows an external subprocess to be naturally placed in a chain of iterables for streaming processing.
Installation
pip install iterable-subprocess
Usage
A single context manager iterable_subprocess
is exposed. The first parameter is the args
argument passed to the Popen Constructor, and the second is an iterable whose items must be bytes
instances and are sent to the subprocess's standard input.
Returned from the function is an iterable whose items are bytes
instances of the process's standard output.
from iterable_subprocess import iterable_subprocess
# In a real case could be a generator function that reads from the filesystem or the network
iterable_of_bytes = (
b'first\n',
b'second\n'
b'third\n'
)
with iterable_subprocess(['cat'], iterable_of_bytes) as output:
for chunk in output:
print(chunk)
Exceptions
Python's subprocess.Popen
is used to start the process, and any exceptions it raises are propagated without transformation. For example, if the subprocess can't be found, then a FileNotFoundError
is raised.
If the process starts, but exits with a non-zero return code, then an iterable_subprocess.IterableSubprocessError
exception will be raised with two members:
returncode
- the return code of the processstderr
- the final 65536 bytes of the standard error of the process
However, if the process starts, but an exception is raised from inside the context, then this exception is propagated, even if the process subsequently exits with a non-zero return code.
Example: unzip the first file of a ZIP archive while downloading
It's possible to download the bytes of a ZIP file in Python, and unzip by passing the bytes to funzip
, as in the following example.
import httpx
from iterable_subprocess import iterable_subprocess
with \
httpx.stream('GET', 'https://www.example.com/my.zip') as r, \
iterable_subprocess(['funzip'], r.iter_bytes()) as unzipped_chunks:
for chunk in unzipped_chunks:
print(chunk)
Note that it's also possible to stream unzip files without resorting to another process using stream-unzip.
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
Hashes for iterable_subprocess-0.0.11.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c972a3626dc2f52001269d91d35e9872b172fb7309d8f95d186535911285e819 |
|
MD5 | b1a352d9d71c9b92d1cd887f399a7e60 |
|
BLAKE2b-256 | db9647fb9b76cca30eaf0926009cd785417f5e3d59743aed68f27d4a498f30f5 |
Hashes for iterable_subprocess-0.0.11-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12882858ed5c07f063ec06127f5805066ad96a6bdd254723312625cbd49b1cb9 |
|
MD5 | 161d5ee0d25e12504aac6de3f78b8ccc |
|
BLAKE2b-256 | e948c13124d2ebb5ca6f0168319b0f8c374bbcdb09ce72930be641310583d4cd |