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 CircleCI Test Coverage

Python utility function 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 function 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

def yield_input():
    # In a real case could read from the filesystem or the network
    yield b'first\n'
    yield b'second\n'
    yield b'third\n'

output = iterable_subprocess(['cat'], yield_input())

for chunk in output:
    print(chunk)

Usage: unzip the first file of a ZIP archive while downloading

While its not typically possible to completely unzip an arbitrary ZIP file on-the-fly, it is possible to unzip the first file in a ZIP archive using funzip, as in the following example.

from iterable_subprocess import iterable_subprocess
import httpx

def zipped_chunks():
    with httpx.stream('GET', 'https://www.example.com/my.zip') as r:
        yield from r.iter_bytes()

unzipped_chunks = iterable_subprocess(['funzip'], zipped_chunks())

for chunk in unzipped_chunks:
    print(chunk)

Ideally Python's zipfile module or Python's zlib module would be able to do this without calling into funzip. However, at the time of writing this does not appear easily possible.

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.6.tar.gz (3.4 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.6-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file iterable-subprocess-0.0.6.tar.gz.

File metadata

  • Download URL: iterable-subprocess-0.0.6.tar.gz
  • Upload date:
  • Size: 3.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.5.0.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.8.5

File hashes

Hashes for iterable-subprocess-0.0.6.tar.gz
Algorithm Hash digest
SHA256 e90087e974488ea5de8329acb7f71ebf7643605a204488d5d7aad01c848c5c38
MD5 fa34f77e237f26d1e7b561421d599736
BLAKE2b-256 cd9f3bac6875926a62c3d1ef7a394f3e4b03451c53b5ba8b0aa9e5c03319462d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: iterable_subprocess-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.5.0.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.8.5

File hashes

Hashes for iterable_subprocess-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 df453760705bbec143330373ed51f685a4625c86e77a19ae089953e4de36030b
MD5 0a5a501c6b7f6d1adb60ceda8dfe39d9
BLAKE2b-256 6368281c9df75c6ef463296260ffc6160f9eb632f0299ad9513f7f2f8c383a6c

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