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 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
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.
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)
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.8.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 420845ad1faa283e6a0a9907fd9de87ba36528c192996f4a485056ccee98b397 |
|
MD5 | d727c0b459d96ad848a485c4b3b94b25 |
|
BLAKE2b-256 | 2436d1454189cfdf21f1dce30f46e3fc1ccdc7107978418f7fdd4039f6225493 |
Hashes for iterable_subprocess-0.0.8-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ce3b1ced79ee8ae0b80c360ae2ed91b924549e564d522bfdebf27257bdc5f6b |
|
MD5 | 909ba5f68cba2fbce8b402418f31e557 |
|
BLAKE2b-256 | 802c4c745e56cfd4959e4a1315a87954905ed8de8f95476158daf5730c2353f8 |