Skip to main content

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

PyPI package Test suite Code coverage

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 process
  • stderr - 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.

Example: download file using curl and process in Python

You would usually download directly from Python, but as an example, you can download using the curl executable and process its output in Python.

from iterable_subprocess import iterable_subprocess

url = 'https://data.api.trade.gov.uk/v1/datasets/uk-tariff-2021-01-01/versions/v3.0.212/tables/measures-on-declarable-commodities/data?format=csv'
with iterable_subprocess(['curl', '--no-progress-meter', '--fail-with-body', url], ()) as output:
    for chunk in output:
        print(chunk)

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

iterable_subprocess-0.0.13.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

iterable_subprocess-0.0.13-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file iterable_subprocess-0.0.13.tar.gz.

File metadata

  • Download URL: iterable_subprocess-0.0.13.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for iterable_subprocess-0.0.13.tar.gz
Algorithm Hash digest
SHA256 689dc9f59849d477452f7d41084f4628289680f758fe76f43a9b4bb0f8c6304e
MD5 88cf90e088cf49b0abde943963983018
BLAKE2b-256 14467a810fe2f883466afa4a8a1d395dc4562300ad3f1411785965f33c324bcc

See more details on using hashes here.

File details

Details for the file iterable_subprocess-0.0.13-py3-none-any.whl.

File metadata

File hashes

Hashes for iterable_subprocess-0.0.13-py3-none-any.whl
Algorithm Hash digest
SHA256 ed89d6fa08093f817fd9dc071716aae8081bb695babed07899ab548d8f702b16
MD5 d29434f67ff26365d42a46e825430c4b
BLAKE2b-256 af94ef49f74c921237d61e3f5c4109f2427bb314a7bf6386be28154b66ecec29

See more details on using hashes here.

Supported by

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