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 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.5.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.5-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: iterable-subprocess-0.0.5.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.5.tar.gz
Algorithm Hash digest
SHA256 7ca8304e88ddee3d5f0d8cf635cbda0a3ed13b95cc62c1e9401bd088d2011bd2
MD5 143345e8b147bb50a7f6b692eb8481a1
BLAKE2b-256 e06a90d369061ae7d967ee373428824be29fcd622e1e8cb6b0a8267eeacc7d62

See more details on using hashes here.

File details

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

File metadata

  • Download URL: iterable_subprocess-0.0.5-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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 3aea70176eaa9d9c7e83de3c2653942f8f00ece9953662d9d7cd8484c6d885ec
MD5 eba4de1a8ee227e44cc8d0e2cd3ca7ff
BLAKE2b-256 a193a496c23a019584514e13608df0dba997172026d8ea572fa45bcfbca9b017

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