subfeed
High-throughput I/O coordinator for feeding persistent subprocesses.
subfeed is a Python library designed to feed stream data into swarms of persistent subprocesses. Unlike standard process pools that focus on executing discrete functions, subfeed focuses on streaming data into long-running external processes via multiple pipes (stdin + side channels) without deadlocks.
It handles the complex mechanics of non-blocking I/O, file descriptor management, and graceful shutdowns, allowing you to focus on your data flow.
Features
- Deadlock-Free I/O: Safely writes to multiple input pipes (e.g., stdin and 3) simultaneously using threaded workers.
- Persistent Processes: "Stoke" long-running processes rather than spawning a new process for every item.
- Multi-Channel Support: Feed distinct data streams to different file descriptors on the same subprocess.
- Lazy Startup: Workers initialize in parallel and come online as ready.
- Graceful Shutdown: Handles the complex sequence of draining buffers, closing pipes, and reaping processes.
Installation
pip install subfeed
Usage Example
In this example, we feed a subprocess that expects data on stdin AND a side channel (File Descriptor 3).
1. The Child Process (child.py)
A script that reads fibonacci numbers from stdin and line numbers from a side channel.
import sys
import os
# Read from stdin (fibonacci sequence)
fibonaccis = sys.stdin
# Read from Side Channel (File Descriptor passed via env var)
fd = int(os.environ["line_numbers"])
line_numbers = open(fd, "r")
for line_number, fib in zip(line_numbers, fibonaccis):
print(f"{line_number.strip()}\t{fib.strip()}")
2. The Coordinator (main.py)
Using subfeed to manage the swarm
from subfeed import *
# Define how to format data for the pipes
class LineNumbersWriter(Writer):
def filter(self, batch):
line_number = str(batch[0]) + "\n"
return line_number.encode()
class TextWriter(Writer):
def filter(self, batch):
fib_line = str(batch[1]) + "\n"
return fib_line.encode()
# 1. Define the Task Template
# We want to run "python child.py"
# We need stdin (implicit) and a side channel named "line_numbers"
template = TaskTemplate(
args="python tests/print_fibonaccis.py",
stdout=FileChannel("{id}.out"), # Write output to 0.out, 1.out...
sidein={"line_numbers": AnonChannel()}
)
# 2. Map data types to channels
specs = {
"stdin": WriterSpec(TextWriter),
"line_numbers": WriterSpec(LineNumbersWriter)
}
# 3. Start the Swarm (2 concurrent processes)
with Coordinator(template, count=2, writer_specs=specs) as swarm:
# Generate first 100 fibonacci sequence and indices
fibs = [0, 1]
for i in range(98):
fibs.append(fibs[-2] + fibs[-1])
expected = {}
for line_number, fib in enumerate(fibs, start = 1):
# The 'feed' method accepts a data object that your Writers understand
expected[line_number] = fib
batch = [line_number, fib]
swarm.feed(batch)
output = open("0.out").readlines() + open("1.out").readlines()
recovered = [[int(i) for i in line.split()] for line in output]
for line_number, fib in recovered:
assert expected[line_number] == fib
Metadata
Release files for subfeed 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| subfeed-0.1.1.tar.gz | 8.6 kB | Without provenance | Details for subfeed-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| subfeed-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for subfeed-0.1.1-py3-none-any.whl |
Total release size: 18.5 kB
Release files / subfeed-0.1.1.tar.gz
| Download URL | subfeed-0.1.1.tar.gz |
|---|---|
| Size | 8.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
00d9da120cd02c733de41195d79362eef9d007d5784d60875d6b83a0aa0dec5f
|
|
BLAKE2b-256 checksum How to use checksums |
cc441608042da5ff0562260fe32dd5270a1db10bbe3964be4c78adb321c28bcc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 colorama/0.4.4 importlib-metadata/7.0.1 keyring/24.3.0 pkginfo/1.9.6 readme-renderer/34.0 requests-toolbelt/1.0.0 requests/2.31.0 rfc3986/1.5.0 tqdm/4.66.1 urllib3/1.26.5 CPython/3.10.12
|
Release files / subfeed-0.1.1-py3-none-any.whl
| Download URL | subfeed-0.1.1-py3-none-any.whl |
|---|---|
| Size | 9.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
86849b38d773c85a7d016cf292e88f49cfe413698a82ef0042554dfb7743160d
|
|
BLAKE2b-256 checksum How to use checksums |
8996792ff46dbc5acf6cb315c32a4a6313251188952f211f4121795f3fb62e5f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 colorama/0.4.4 importlib-metadata/7.0.1 keyring/24.3.0 pkginfo/1.9.6 readme-renderer/34.0 requests-toolbelt/1.0.0 requests/2.31.0 rfc3986/1.5.0 tqdm/4.66.1 urllib3/1.26.5 CPython/3.10.12
|
Security for subfeed 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.