Skip to content
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

fix: replace pkg_resources with importlib #563

Merged
merged 6 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ jobs:
- name: install pyright
run: npm install -g pyright
- name: Lint
run: make lint
run: make lint semantic_lint

test:
runs-on: ${{ matrix.os }}
timeout-minutes: 20
strategy:
max-parallel: 10
max-parallel: 18
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
os: [ubuntu-latest , macos-13, macos-14]
exclude:
- python-version: "3.8"
Expand All @@ -73,22 +73,27 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
allow-prereleases: true
- name: Set up Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: nightly
- name: Install dependencies
- name: Install components
run: |
python -m pip install --upgrade pip
pip install -e .[dev,mixin]
rustup component add clippy
make dev
- name: Test unit
# ignore the mixin test for Python 3.13 since some of the dependencies are not ready
if: ${{ startsWith(matrix.python-version, '3.13')}}
run: |
make test_unit
- name: Test
if: ${{ !startsWith(matrix.python-version, '3.13')}}
run: |
make semantic_lint test
make test
- name: Test shm in Linux
# ignore the shm test for Python 3.12 since pyarrow doesn't have py3.12 wheel with version < 12
if: ${{ startsWith(matrix.os, 'ubuntu') && !startsWith(matrix.python-version, '3.12') }}
if: ${{ startsWith(matrix.os, 'ubuntu') && !startsWith(matrix.python-version, '3.12') && !startsWith(matrix.python-version, '3.13') }}
run: |
sudo apt update && sudo apt install redis
make test_shm
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ dev:
cargo build
@mkdir -p mosec/bin
@cp ./target/debug/mosec mosec/bin/mosec
pip install -e .
pip install -e .[dev]

test: dev
echo "Running tests for the main logic"
@pip install -q -r requirements/mixin.txt
echo "Running tests for the main logic and mixin(!shm)"
pytest tests -vv -s -m "not shm"
RUST_BACKTRACE=1 cargo test -vv

test_unit: dev
echo "Running tests for the main logic"
pytest -vv -s tests/test_log.py tests/test_protocol.py tests/test_coordinator.py
RUST_BACKTRACE=1 cargo test -vv

test_shm: dev
@pip install -q -r requirements/mixin.txt
echo "Running tests for the shm mixin"
Expand Down
4 changes: 2 additions & 2 deletions mosec/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import json
import logging
import os
from datetime import datetime
from datetime import datetime, timezone
from typing import Any, MutableMapping

from mosec.args import get_log_level
Expand All @@ -42,7 +42,7 @@ class MosecFormat(logging.Formatter):

def formatTime(self, record: logging.LogRecord, datefmt=None) -> str:
"""Convert to datetime with timezone."""
time = datetime.fromtimestamp(record.created).utcnow()
time = datetime.fromtimestamp(record.created).now(timezone.utc)
return datetime.strftime(time, datefmt if datefmt else self.default_time_format)


Expand Down
15 changes: 11 additions & 4 deletions mosec/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,23 @@

import multiprocessing as mp
import subprocess
import sys
from multiprocessing.context import ForkContext, SpawnContext
from multiprocessing.process import BaseProcess
from multiprocessing.synchronize import Event
from pathlib import Path
from time import monotonic, sleep
from typing import Callable, Dict, Iterable, List, Optional, Type, Union, cast

import pkg_resources
if sys.version_info >= (3, 9):
from importlib.resources import files as importlib_files
else:
from pkg_resources import resource_filename

def importlib_files(package: str) -> Path:
"""Get the resource file path."""
return Path(resource_filename(package, ""))


from mosec.coordinator import Coordinator
from mosec.env import env_var_context, validate_env, validate_int_ge
Expand Down Expand Up @@ -239,9 +248,7 @@ def __init__(self, timeout: int):
"""
self.process: Optional[subprocess.Popen] = None

self.server_path = Path(
pkg_resources.resource_filename("mosec", "bin"), "mosec"
)
self.server_path = importlib_files("mosec") / "bin" / "mosec"
self.timeout = timeout

def halt(self):
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ license = {text = "Apache-2.0"}
keywords = ["machine learning", "deep learning", "model serving"]
dynamic = ["version"]
requires-python = ">=3.8"
dependencies = []
classifiers = [
"Environment :: GPU",
"Intended Audience :: Developers",
Expand Down Expand Up @@ -80,8 +81,6 @@ markers = [
"shm: mark a test is related to shared memory",
]

[tool.ruff]
target-version = "py38"
[tool.ruff.lint]
select = ["E", "F", "G", "B", "I", "SIM", "TID", "PL", "RUF", "D"]
ignore = ["E501", "D203", "D213"]
Expand Down
2 changes: 0 additions & 2 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@ mypy~=1.11
pyright~=1.1
ruff~=0.6
pre-commit>=2.15.0
msgpack>=1.0.5
numpy<2
httpx==0.27.2
httpx-sse==0.4.0
1 change: 1 addition & 0 deletions requirements/mixin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ msgspec>=0.15.0
numbin>=0.5.0
pyarrow>=0.6.1,<12; python_version < "3.12"
redis>=4.0.0
numpy<2 # pyarrow legacy dependency
14 changes: 0 additions & 14 deletions tests/test_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@
from os.path import join
from typing import Union, cast

import msgpack # type: ignore
import pytest

from mosec.coordinator import PROTOCOL_TIMEOUT, Coordinator, State
from mosec.mixin import MsgpackMixin
from mosec.protocol import HTTPStatusCode, _recv_all
from mosec.worker import Worker
from tests.utils import imitate_controller_send
Expand Down Expand Up @@ -65,10 +63,6 @@ def forward(self, data):
return data


class EchoWorkerMsgPack(MsgpackMixin, EchoWorkerJSON):
pass


@pytest.fixture
def base_test_config():
return {
Expand Down Expand Up @@ -192,14 +186,6 @@ def test_incorrect_socket_file(mocker, base_test_config, caplog):
EchoWorkerJSON,
json.loads,
),
(
[
msgpack.packb({"rid": "147982364", "data": b"im_bytes"}),
msgpack.packb({"rid": "147982831", "data": b"another_im_bytes"}),
],
EchoWorkerMsgPack,
msgpack.unpackb,
),
],
)
def test_echo_batch(base_test_config, test_data, worker, deserializer):
Expand Down