-
Notifications
You must be signed in to change notification settings - Fork 24
refactor(pynumaflow-lite): Pythonic APIs for BatchMapper #382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d3f1390
feat(pynumaflow-lite): Pythonic APIs for batchmapper
BulkBeing e0a1d5c
formatting
BulkBeing a62ad70
fix tests and examples
BulkBeing d6fb3eb
formatting
BulkBeing 88dc279
avoid tests hanging when cargo compilation fails
BulkBeing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| .venv/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,37 +1,43 @@ | ||
| FROM python:3.11-slim-bullseye AS builder | ||
|
|
||
| ENV PYTHONFAULTHANDLER=1 \ | ||
| PYTHONUNBUFFERED=1 \ | ||
| PYTHONHASHSEED=random \ | ||
| PIP_NO_CACHE_DIR=on \ | ||
| PIP_DISABLE_PIP_VERSION_CHECK=on \ | ||
| PIP_DEFAULT_TIMEOUT=100 \ | ||
| POETRY_HOME="/opt/poetry" \ | ||
| POETRY_VIRTUALENVS_IN_PROJECT=true \ | ||
| POETRY_NO_INTERACTION=1 \ | ||
| PYSETUP_PATH="/opt/pysetup" | ||
|
|
||
| ENV PATH="$POETRY_HOME/bin:$PATH" | ||
|
|
||
| RUN apt-get update \ | ||
| && apt-get install --no-install-recommends -y \ | ||
| curl \ | ||
| wget \ | ||
| # deps for building python deps | ||
| build-essential \ | ||
| && apt-get install -y git \ | ||
| && apt-get clean && rm -rf /var/lib/apt/lists/* \ | ||
| && curl -sSL https://install.python-poetry.org | python3 - | ||
|
|
||
| FROM builder AS udf | ||
|
|
||
| WORKDIR $PYSETUP_PATH | ||
| COPY ./ ./ | ||
|
|
||
| RUN pip install $PYSETUP_PATH/pynumaflow_lite-0.1.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl | ||
|
|
||
| RUN poetry lock | ||
| RUN poetry install --no-cache --no-root && \ | ||
| rm -rf ~/.cache/pypoetry/ | ||
|
|
||
| CMD ["python", "batchmap_cat.py"] | ||
| FROM python:3.11-slim-trixie AS builder | ||
|
|
||
| COPY --from=ghcr.io/astral-sh/uv:0.12.13 /uv /uvx /bin/ | ||
|
|
||
| ENV UV_COMPILE_BYTECODE=1 \ | ||
| UV_LINK_MODE=copy \ | ||
| UV_PYTHON_DOWNLOADS=never | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| RUN --mount=type=cache,target=/root/.cache/uv \ | ||
| --mount=type=bind,source=uv.lock,target=uv.lock \ | ||
| --mount=type=bind,source=pyproject.toml,target=pyproject.toml \ | ||
| uv sync --locked --no-install-project --no-dev | ||
|
|
||
| COPY . . | ||
|
|
||
| RUN --mount=type=cache,target=/root/.cache/uv \ | ||
| uv sync --locked --no-dev | ||
|
|
||
| # Install the local pynumaflow-lite wheel that matches the build platform. | ||
| RUN uv pip install --no-index --find-links . pynumaflow-lite | ||
|
|
||
| FROM python:3.11-slim-trixie | ||
|
|
||
| # Setup a non-root user | ||
| RUN groupadd --system --gid 999 nonroot \ | ||
| && useradd --system --gid 999 --uid 999 --create-home nonroot | ||
|
|
||
| COPY --from=builder --chown=nonroot:nonroot /app /app | ||
|
|
||
| ENV PATH="/app/.venv/bin:$PATH" | ||
|
|
||
| # Keeps Python from buffering stdout and stderr to avoid situations where | ||
| # the application crashes without emitting any logs due to buffering. | ||
| ENV PYTHONUNBUFFERED=1 | ||
|
|
||
| # Use the non-root user to run our application | ||
| USER nonroot | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| CMD ["python", "batchmap_cat.py"] |
52 changes: 16 additions & 36 deletions
52
packages/pynumaflow-lite/manifests/batchmap/batchmap_cat.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,46 +1,26 @@ | ||
| import asyncio | ||
| import signal | ||
| from collections.abc import AsyncIterable, Awaitable, Callable | ||
| from collections.abc import AsyncIterable | ||
|
|
||
| from pynumaflow_lite import batchmapper | ||
| from pynumaflow_lite.batchmapper import Message | ||
| from pynumaflow_lite.batchmapper import BatchMapAsyncServer, BatchMapper, BatchResponse, Datum, Message | ||
|
|
||
|
|
||
| class SimpleBatchCat(batchmapper.BatchMapper): | ||
| async def handler(self, batch: AsyncIterable[batchmapper.Datum]) -> batchmapper.BatchResponses: | ||
| responses = batchmapper.BatchResponses() | ||
| async for d in batch: | ||
| resp = batchmapper.BatchResponse(d.id) | ||
| if d.value == b"bad world": | ||
| resp.append(Message.message_to_drop()) | ||
| continue | ||
|
|
||
| resp.append(Message(d.value, d.keys)) | ||
| responses.append(resp) | ||
| class SimpleBatchCat(BatchMapper): | ||
| async def handler(self, batch: AsyncIterable[Datum]) -> list[BatchResponse]: | ||
| responses = [] | ||
| async for datum in batch: | ||
| if datum.value == b"bad world": | ||
| responses.append(BatchResponse(datum.id, Message.to_drop())) | ||
| else: | ||
| responses.append(BatchResponse(datum.id, Message(datum.value, keys=datum.keys))) | ||
| return responses | ||
|
|
||
|
|
||
| async def start( | ||
| f: Callable[[AsyncIterable[batchmapper.Datum]], Awaitable[batchmapper.BatchResponses]], | ||
| ): | ||
| server = batchmapper.BatchMapAsyncServer() | ||
|
|
||
| # Register loop-level signal handlers so we control shutdown and avoid asyncio.run | ||
| loop = asyncio.get_running_loop() | ||
| try: | ||
| loop.add_signal_handler(signal.SIGINT, lambda: server.stop()) | ||
| loop.add_signal_handler(signal.SIGTERM, lambda: server.stop()) | ||
| except (NotImplementedError, RuntimeError): | ||
| pass | ||
|
|
||
| try: | ||
| await server.start(f) | ||
| print("Shutting down gracefully...") | ||
| except asyncio.CancelledError: | ||
| server.stop() | ||
| return | ||
| async def main() -> None: | ||
| print("Starting BatchMap server") | ||
| # `serve` returns when SIGINT or SIGTERM arrives. | ||
| await BatchMapAsyncServer(SimpleBatchCat()).serve() | ||
| print("BatchMap server stopped") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| async_handler = SimpleBatchCat() | ||
| asyncio.run(start(async_handler)) | ||
| asyncio.run(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all names are already added in as a static list above