pipestage 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pipestage-0.1.0/.gitignore +237 -0
- pipestage-0.1.0/CHANGELOG.md +22 -0
- pipestage-0.1.0/LICENSE +21 -0
- pipestage-0.1.0/PKG-INFO +359 -0
- pipestage-0.1.0/README.md +333 -0
- pipestage-0.1.0/examples/compare_batch.py +68 -0
- pipestage-0.1.0/examples/compare_embed.py +57 -0
- pipestage-0.1.0/examples/compare_fanout.py +86 -0
- pipestage-0.1.0/examples/compare_fetch.py +67 -0
- pipestage-0.1.0/examples/compare_files.py +53 -0
- pipestage-0.1.0/examples/compare_notify.py +56 -0
- pipestage-0.1.0/examples/compare_paginated.py +50 -0
- pipestage-0.1.0/examples/compare_resilient.py +51 -0
- pipestage-0.1.0/examples/ps_batch.py +55 -0
- pipestage-0.1.0/examples/ps_embed.py +78 -0
- pipestage-0.1.0/examples/ps_fanout.py +68 -0
- pipestage-0.1.0/examples/ps_fetch.py +56 -0
- pipestage-0.1.0/examples/ps_files.py +67 -0
- pipestage-0.1.0/examples/ps_notify.py +58 -0
- pipestage-0.1.0/examples/ps_paginated.py +54 -0
- pipestage-0.1.0/examples/ps_resilient.py +60 -0
- pipestage-0.1.0/examples/raw_batch.py +68 -0
- pipestage-0.1.0/examples/raw_embed.py +102 -0
- pipestage-0.1.0/examples/raw_fanout.py +85 -0
- pipestage-0.1.0/examples/raw_fetch.py +66 -0
- pipestage-0.1.0/examples/raw_files.py +85 -0
- pipestage-0.1.0/examples/raw_notify.py +60 -0
- pipestage-0.1.0/examples/raw_paginated.py +61 -0
- pipestage-0.1.0/examples/raw_resilient.py +59 -0
- pipestage-0.1.0/examples/run_all.py +39 -0
- pipestage-0.1.0/pyproject.toml +65 -0
- pipestage-0.1.0/src/pipestage/__init__.py +13 -0
- pipestage-0.1.0/src/pipestage/_ops.py +146 -0
- pipestage-0.1.0/src/pipestage/_stream.py +73 -0
- pipestage-0.1.0/src/pipestage/_utils.py +24 -0
- pipestage-0.1.0/src/pipestage/py.typed +0 -0
- pipestage-0.1.0/tests/__init__.py +0 -0
- pipestage-0.1.0/tests/test_basic.py +153 -0
- pipestage-0.1.0/tests/test_concurrency.py +141 -0
- pipestage-0.1.0/tests/test_errors.py +117 -0
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
# Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
# uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
# poetry.lock
|
|
109
|
+
# poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
# pdm.lock
|
|
116
|
+
# pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
# pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# Redis
|
|
135
|
+
*.rdb
|
|
136
|
+
*.aof
|
|
137
|
+
*.pid
|
|
138
|
+
|
|
139
|
+
# RabbitMQ
|
|
140
|
+
mnesia/
|
|
141
|
+
rabbitmq/
|
|
142
|
+
rabbitmq-data/
|
|
143
|
+
|
|
144
|
+
# ActiveMQ
|
|
145
|
+
activemq-data/
|
|
146
|
+
|
|
147
|
+
# SageMath parsed files
|
|
148
|
+
*.sage.py
|
|
149
|
+
|
|
150
|
+
# Environments
|
|
151
|
+
.env
|
|
152
|
+
.envrc
|
|
153
|
+
.venv
|
|
154
|
+
env/
|
|
155
|
+
venv/
|
|
156
|
+
ENV/
|
|
157
|
+
env.bak/
|
|
158
|
+
venv.bak/
|
|
159
|
+
|
|
160
|
+
# Spyder project settings
|
|
161
|
+
.spyderproject
|
|
162
|
+
.spyproject
|
|
163
|
+
|
|
164
|
+
# Rope project settings
|
|
165
|
+
.ropeproject
|
|
166
|
+
|
|
167
|
+
# mkdocs documentation
|
|
168
|
+
/site
|
|
169
|
+
|
|
170
|
+
# mypy
|
|
171
|
+
.mypy_cache/
|
|
172
|
+
.dmypy.json
|
|
173
|
+
dmypy.json
|
|
174
|
+
|
|
175
|
+
# Pyre type checker
|
|
176
|
+
.pyre/
|
|
177
|
+
|
|
178
|
+
# pytype static type analyzer
|
|
179
|
+
.pytype/
|
|
180
|
+
|
|
181
|
+
# Cython debug symbols
|
|
182
|
+
cython_debug/
|
|
183
|
+
|
|
184
|
+
# PyCharm
|
|
185
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
186
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
188
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
189
|
+
# .idea/
|
|
190
|
+
|
|
191
|
+
# Abstra
|
|
192
|
+
# Abstra is an AI-powered process automation framework.
|
|
193
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
194
|
+
# Learn more at https://abstra.io/docs
|
|
195
|
+
.abstra/
|
|
196
|
+
|
|
197
|
+
# Visual Studio Code
|
|
198
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
199
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
200
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
201
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
202
|
+
# .vscode/
|
|
203
|
+
# Temporary file for partial code execution
|
|
204
|
+
tempCodeRunnerFile.py
|
|
205
|
+
|
|
206
|
+
# Ruff stuff:
|
|
207
|
+
.ruff_cache/
|
|
208
|
+
|
|
209
|
+
# PyPI configuration file
|
|
210
|
+
.pypirc
|
|
211
|
+
|
|
212
|
+
# Marimo
|
|
213
|
+
marimo/_static/
|
|
214
|
+
marimo/_lsp/
|
|
215
|
+
__marimo__/
|
|
216
|
+
|
|
217
|
+
# Streamlit
|
|
218
|
+
.streamlit/secrets.toml
|
|
219
|
+
|
|
220
|
+
# Build artifacts — never commit these
|
|
221
|
+
dist/
|
|
222
|
+
build/
|
|
223
|
+
*.egg-info/
|
|
224
|
+
|
|
225
|
+
# Local development notes — not for public repo
|
|
226
|
+
localdev/
|
|
227
|
+
|
|
228
|
+
# HTML docs — hosted on openlabx.com, not in the repo
|
|
229
|
+
docs/
|
|
230
|
+
|
|
231
|
+
# Word and compiled HTML exports in docs
|
|
232
|
+
docs/*.docx
|
|
233
|
+
docs/all.htm
|
|
234
|
+
|
|
235
|
+
# Editor
|
|
236
|
+
.vscode/
|
|
237
|
+
.idea/
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to pipestage will be documented here.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## [0.1.0] - 2026-06-25
|
|
8
|
+
|
|
9
|
+
First release.
|
|
10
|
+
|
|
11
|
+
### Added
|
|
12
|
+
|
|
13
|
+
- `stream(source)` - entry point accepting any sync or async iterable
|
|
14
|
+
- `Stream.map(fn, *, concurrency, ordered)` - serial and concurrent map with ordered/unordered output
|
|
15
|
+
- `Stream.filter(pred, *, concurrency, ordered)` - serial and concurrent filter
|
|
16
|
+
- `Stream.flat_map(fn, *, concurrency, ordered)` - map then flatten one level
|
|
17
|
+
- `Stream.batch(size)` - group items into fixed-size lists
|
|
18
|
+
- `Stream.collect()` - terminal, returns `list[T]`
|
|
19
|
+
- `Stream.for_each(fn, *, concurrency, ordered)` - terminal, side-effect consumer
|
|
20
|
+
- Async iteration - `Stream` usable directly in `async for` loops
|
|
21
|
+
- Fail-fast error handling - first exception cancels in-flight tasks and propagates unchanged
|
|
22
|
+
- Zero runtime dependencies, Python 3.11+
|
pipestage-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 OpenLabX
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
pipestage-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pipestage
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Python library for building async data pipelines with safe staged processing and bounded concurrency
|
|
5
|
+
Project-URL: Homepage, https://github.com/openlab-x/pipestage
|
|
6
|
+
Project-URL: Repository, https://github.com/openlab-x/pipestage
|
|
7
|
+
License: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Keywords: async,asyncio,concurrency,data,pipeline,stream
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Framework :: AsyncIO
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Typing :: Typed
|
|
19
|
+
Requires-Python: >=3.11
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: mypy>=1.10; extra == 'dev'
|
|
22
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
|
|
23
|
+
Requires-Dist: pytest>=8.0; extra == 'dev'
|
|
24
|
+
Requires-Dist: ruff>=0.4; extra == 'dev'
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# pipestage
|
|
28
|
+
|
|
29
|
+
<div align="center">
|
|
30
|
+
<img src="https://raw.githubusercontent.com/openlab-x/pipestage/main/assets/logo.png" width="400"/>
|
|
31
|
+
|
|
32
|
+

|
|
33
|
+

|
|
34
|
+

|
|
35
|
+

|
|
36
|
+
|
|
37
|
+
</div>
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from pipestage import stream
|
|
41
|
+
|
|
42
|
+
await (
|
|
43
|
+
stream(documents)
|
|
44
|
+
.flat_map(chunk, concurrency=8)
|
|
45
|
+
.map(embed, concurrency=16)
|
|
46
|
+
.batch(100)
|
|
47
|
+
.for_each(upsert, concurrency=4)
|
|
48
|
+
)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## About
|
|
52
|
+
|
|
53
|
+
pipestage is an open-source Python library for building async data pipelines with safe staged processing and bounded concurrency. It is developed by OpenLabX and built entirely on the Python standard library with zero runtime dependencies.
|
|
54
|
+
|
|
55
|
+
The key difference from raw `asyncio.gather()`: gather is a barrier - stage 2 cannot start until every item from stage 1 is done. pipestage stages are lazy async generators that overlap in time, so stage 2 starts consuming stage 1's output as soon as the first item is ready.
|
|
56
|
+
|
|
57
|
+
pipestage is aimed at crawlers, ingestion jobs, API fan-out, file processing, and LLM data pipelines.
|
|
58
|
+
|
|
59
|
+
## Table of Contents
|
|
60
|
+
|
|
61
|
+
- [About](#about)
|
|
62
|
+
- [Features](#features)
|
|
63
|
+
- [Install](#install)
|
|
64
|
+
- [Quick Start](#quick-start)
|
|
65
|
+
- [API](#api)
|
|
66
|
+
- [stream()](#stream)
|
|
67
|
+
- [.map()](#map)
|
|
68
|
+
- [.filter()](#filter)
|
|
69
|
+
- [.flat_map()](#flat_map)
|
|
70
|
+
- [.batch()](#batch)
|
|
71
|
+
- [.collect()](#collect)
|
|
72
|
+
- [.for_each()](#for_each)
|
|
73
|
+
- [Async iteration](#async-iteration)
|
|
74
|
+
- [Error Handling](#error-handling)
|
|
75
|
+
- [Examples](#examples)
|
|
76
|
+
- [Project Structure](#project-structure)
|
|
77
|
+
- [Architecture](#architecture)
|
|
78
|
+
- [Concurrency Model](#concurrency-model)
|
|
79
|
+
- [Dependencies](#dependencies)
|
|
80
|
+
- [Python Versions Tested](#python-versions-tested)
|
|
81
|
+
- [Source Code Version 0.1.0](#source-code-version-010)
|
|
82
|
+
- [Known Issues at v0.1.0](#known-issues-at-v010)
|
|
83
|
+
- [Contributing](#contributing)
|
|
84
|
+
- [License](#license)
|
|
85
|
+
- [Contact](#contact)
|
|
86
|
+
|
|
87
|
+
## Features
|
|
88
|
+
|
|
89
|
+
- **Stage Overlap**: All stages run simultaneously. No gather barriers between steps.
|
|
90
|
+
- **Bounded Concurrency**: Per-stage semaphore. Set concurrency once, the rest is handled.
|
|
91
|
+
- **Ordered or Unordered**: Preserve input order, or emit results as ready. One flag.
|
|
92
|
+
- **Sync and Async**: Pass sync or async functions anywhere. No wrapping required.
|
|
93
|
+
- **Async Generator Source**: `stream()` accepts any async iterable directly.
|
|
94
|
+
- **Fail-Fast Error Handling**: First exception cancels in-flight tasks and propagates unchanged.
|
|
95
|
+
- **Zero Dependencies**: Pure Python standard library. Nothing to install except pipestage itself.
|
|
96
|
+
|
|
97
|
+
## Install
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
git clone https://github.com/openlab-x/pipestage.git
|
|
101
|
+
cd pipestage
|
|
102
|
+
pip install -e .
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Requires Python 3.11 or later. No runtime dependencies. PyPI release coming soon.
|
|
106
|
+
|
|
107
|
+
## Quick Start
|
|
108
|
+
|
|
109
|
+
**Concurrent fetch and parse:**
|
|
110
|
+
```python
|
|
111
|
+
from pipestage import stream
|
|
112
|
+
|
|
113
|
+
results = await (
|
|
114
|
+
stream(urls)
|
|
115
|
+
.map(fetch, concurrency=20)
|
|
116
|
+
.filter(lambda r: r["status"] == 200)
|
|
117
|
+
.map(parse, concurrency=8)
|
|
118
|
+
.collect()
|
|
119
|
+
)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
**Batch DB inserts:**
|
|
123
|
+
```python
|
|
124
|
+
await (
|
|
125
|
+
stream(records)
|
|
126
|
+
.map(transform, concurrency=16)
|
|
127
|
+
.batch(100)
|
|
128
|
+
.for_each(insert_batch, concurrency=4)
|
|
129
|
+
)
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
**LLM fan-out - emit as ready:**
|
|
133
|
+
```python
|
|
134
|
+
results = await (
|
|
135
|
+
stream(prompts)
|
|
136
|
+
.map(call_llm, concurrency=8, ordered=False)
|
|
137
|
+
.collect()
|
|
138
|
+
)
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
**Async generator source:**
|
|
142
|
+
```python
|
|
143
|
+
results = await (
|
|
144
|
+
stream(scan_directory())
|
|
145
|
+
.map(read_file, concurrency=10)
|
|
146
|
+
.filter(lambda f: f["word_count"] >= 500)
|
|
147
|
+
.collect()
|
|
148
|
+
)
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## API
|
|
152
|
+
|
|
153
|
+
### stream()
|
|
154
|
+
|
|
155
|
+
Create a pipeline from any sync or async iterable.
|
|
156
|
+
|
|
157
|
+
```python
|
|
158
|
+
stream([1, 2, 3])
|
|
159
|
+
stream(range(1000))
|
|
160
|
+
stream(async_generator())
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### .map()
|
|
164
|
+
|
|
165
|
+
```python
|
|
166
|
+
.map(fn, *, concurrency=1, ordered=True)
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Apply `fn` to every item. `fn` may be sync or async. `ordered=False` emits results as they finish instead of preserving input order.
|
|
170
|
+
|
|
171
|
+
### .filter()
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
.filter(pred, *, concurrency=1, ordered=True)
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
Keep only items for which `pred` returns truthy.
|
|
178
|
+
|
|
179
|
+
### .flat_map()
|
|
180
|
+
|
|
181
|
+
```python
|
|
182
|
+
.flat_map(fn, *, concurrency=1, ordered=True)
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
Map `fn` over each item then flatten one level. `fn` should return a list, generator, or async iterable.
|
|
186
|
+
|
|
187
|
+
### .batch()
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
.batch(size)
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
Group items into lists of at most `size` elements. The final batch may be smaller.
|
|
194
|
+
|
|
195
|
+
### .collect()
|
|
196
|
+
|
|
197
|
+
Terminal. Consume the pipeline and return all results as a list.
|
|
198
|
+
|
|
199
|
+
```python
|
|
200
|
+
results = await stream(items).map(fn).collect()
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### .for_each()
|
|
204
|
+
|
|
205
|
+
```python
|
|
206
|
+
.for_each(fn, *, concurrency=1, ordered=True)
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Terminal. Consume the pipeline calling `fn` on each item for side effects. Return values are discarded.
|
|
210
|
+
|
|
211
|
+
### Async iteration
|
|
212
|
+
|
|
213
|
+
`Stream` is an async iterable. Use it directly without `collect()`.
|
|
214
|
+
|
|
215
|
+
```python
|
|
216
|
+
async for item in stream(records).map(transform, concurrency=8):
|
|
217
|
+
print(item)
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
## Error Handling
|
|
221
|
+
|
|
222
|
+
The pipeline fails fast by default. The first exception stops the pipeline, cancels in-flight tasks, and propagates the original exception unchanged to the caller.
|
|
223
|
+
|
|
224
|
+
```python
|
|
225
|
+
try:
|
|
226
|
+
await stream(urls).map(fetch, concurrency=10).collect()
|
|
227
|
+
except RuntimeError as e:
|
|
228
|
+
print(e)
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
To continue past individual failures, handle exceptions inside `fn`:
|
|
232
|
+
|
|
233
|
+
```python
|
|
234
|
+
async def safe_fetch(url):
|
|
235
|
+
try:
|
|
236
|
+
return await fetch(url)
|
|
237
|
+
except Exception:
|
|
238
|
+
return None
|
|
239
|
+
|
|
240
|
+
results = await (
|
|
241
|
+
stream(urls)
|
|
242
|
+
.map(safe_fetch, concurrency=10)
|
|
243
|
+
.filter(lambda r: r is not None)
|
|
244
|
+
.collect()
|
|
245
|
+
)
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
## Examples
|
|
249
|
+
|
|
250
|
+
Each feature in `examples/` has three files: a raw asyncio implementation, a pipestage implementation, and a compare script that runs both and prints timing and line-count metrics.
|
|
251
|
+
|
|
252
|
+
```bash
|
|
253
|
+
python examples/compare_embed.py # single comparison
|
|
254
|
+
python examples/run_all.py # all eight
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
| # | Example | Features | Key result |
|
|
258
|
+
|---|---------|----------|------------|
|
|
259
|
+
| 1 | Fetch and Parse | map, filter | Stage overlap: ~1.7x speedup |
|
|
260
|
+
| 2 | Batch Inserts | map, batch, for_each | ~1.6x speedup |
|
|
261
|
+
| 3 | Fan-out | ordered=False | 6x lower time-to-first-result |
|
|
262
|
+
| 4 | Paginated Search | flat_map | Replaces gather + nested loop |
|
|
263
|
+
| 5 | Resilient Calls | error handling | Error logic stays in fn |
|
|
264
|
+
| 6 | Notifications | for_each | No Lock needed for shared state |
|
|
265
|
+
| 7 | File Processing | async generator source | Streams without materializing |
|
|
266
|
+
| 8 | RAG Pipeline | flat_map, map, batch, for_each | All stages overlap simultaneously |
|
|
267
|
+
|
|
268
|
+
## Project Structure
|
|
269
|
+
|
|
270
|
+
```
|
|
271
|
+
src/pipestage/
|
|
272
|
+
__init__.py - public entry point: stream()
|
|
273
|
+
_stream.py - Stream class, fluent API
|
|
274
|
+
_ops.py - async generator stages
|
|
275
|
+
_utils.py - internal helpers
|
|
276
|
+
tests/
|
|
277
|
+
test_basic.py
|
|
278
|
+
test_concurrency.py
|
|
279
|
+
test_errors.py
|
|
280
|
+
examples/
|
|
281
|
+
raw_X.py - plain asyncio implementations
|
|
282
|
+
ps_X.py - pipestage implementations
|
|
283
|
+
compare_X.py - timing comparisons
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
### Architecture
|
|
287
|
+
|
|
288
|
+
Every transformation returns a new `Stream` wrapping an async generator. Nothing executes until `collect()` or `for_each()` is awaited. Each stage pulls from the previous one - all stages run simultaneously.
|
|
289
|
+
|
|
290
|
+
### Concurrency Model
|
|
291
|
+
|
|
292
|
+
| concurrency | ordered | Behavior |
|
|
293
|
+
|---|---|---|
|
|
294
|
+
| 1 | any | Serial. No tasks created. |
|
|
295
|
+
| > 1 | True | Semaphore limits active tasks. Results in input order. |
|
|
296
|
+
| > 1 | False | Results emitted via Queue as tasks complete. |
|
|
297
|
+
|
|
298
|
+
## Dependencies
|
|
299
|
+
|
|
300
|
+
**Runtime:** none.
|
|
301
|
+
|
|
302
|
+
**Development:**
|
|
303
|
+
```bash
|
|
304
|
+
pip install -e ".[dev]"
|
|
305
|
+
# installs: pytest, pytest-asyncio, ruff, mypy
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
## Python Versions Tested
|
|
309
|
+
|
|
310
|
+
- [x] **Python 3.11**
|
|
311
|
+
- [x] **Python 3.12**
|
|
312
|
+
- [x] **Python 3.13**
|
|
313
|
+
- [x] **Python 3.14**
|
|
314
|
+
|
|
315
|
+
## Source Code Version 0.1.0
|
|
316
|
+
|
|
317
|
+
- **Core pipeline**: `stream()`, `map`, `filter`, `flat_map`, `batch`, `collect`, `for_each`
|
|
318
|
+
- **Concurrent execution**: ordered and unordered modes with `asyncio.Semaphore`
|
|
319
|
+
- **Async iteration**: `Stream` usable directly in `async for` loops
|
|
320
|
+
- **Full test suite**: 44 tests across correctness, concurrency, and error propagation
|
|
321
|
+
- **Zero runtime dependencies**: Python 3.11+ standard library only
|
|
322
|
+
|
|
323
|
+
## Known Issues at v0.1.0
|
|
324
|
+
|
|
325
|
+
- All source items are consumed upfront before any results are yielded. For very large sources this can accumulate many Task objects in memory.
|
|
326
|
+
- No per-item timeout. A hung `fn` call blocks its concurrency slot indefinitely.
|
|
327
|
+
- If a consumer breaks out of `async for` early, in-flight tasks keep running until the event loop cleans them up.
|
|
328
|
+
|
|
329
|
+
## Contributing
|
|
330
|
+
|
|
331
|
+
We welcome contributions.
|
|
332
|
+
|
|
333
|
+
1. Give the project a star.
|
|
334
|
+
2. Follow us on GitHub.
|
|
335
|
+
3. Fork the repository.
|
|
336
|
+
4. Create a new branch for your feature or fix.
|
|
337
|
+
5. Make your changes and add tests.
|
|
338
|
+
6. Submit a pull request.
|
|
339
|
+
|
|
340
|
+
## License
|
|
341
|
+
|
|
342
|
+
This project is licensed under the [MIT License](LICENSE).
|
|
343
|
+
|
|
344
|
+
## Contact
|
|
345
|
+
|
|
346
|
+
In pursuit of innovation,
|
|
347
|
+
**OpenLabX Team**
|
|
348
|
+
|
|
349
|
+
- **Website**: [https://openlabx.com](https://openlabx.com)
|
|
350
|
+
- **Email**: contact@openlabx.com
|
|
351
|
+
|
|
352
|
+
**Follow Us:**
|
|
353
|
+
|
|
354
|
+
<div align="center">
|
|
355
|
+
|
|
356
|
+
| <a href="https://www.instagram.com/openlabx_official/" target="_blank"><strong>Instagram</strong></a> | <a href="https://x.com/openlabx" target="_blank"><strong>X (formerly Twitter)</strong></a> | <a href="https://www.facebook.com/openlabx/" target="_blank"><strong>Facebook</strong></a> | <a href="https://www.youtube.com/@OpenLabX" target="_blank"><strong>YouTube</strong></a> | <a href="https://github.com/openlab-x" target="_blank"><strong>GitHub</strong></a> |
|
|
357
|
+
|---|---|---|---|---|
|
|
358
|
+
|
|
359
|
+
</div>
|