Skip to content

Commit 4ce3e1a

Browse files
authored
fix: replace pkg_resources with importlib (#563)
* fix: replace pkg_resources with importlib in std Signed-off-by: Keming <[email protected]> * detect py version when import Signed-off-by: Keming <[email protected]> * skip mixin test for 3.13 Signed-off-by: Keming <[email protected]> * fix py dependencies Signed-off-by: Keming <[email protected]> * move numpy to mixin requirements Signed-off-by: Keming <[email protected]> * fix numpy to <2 since pyarrow requires numpy1.x Signed-off-by: Keming <[email protected]> --------- Signed-off-by: Keming <[email protected]>
1 parent 1fad252 commit 4ce3e1a

File tree

8 files changed

+36
-34
lines changed

8 files changed

+36
-34
lines changed

.github/workflows/check.yml

+13-8
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ jobs:
4949
- name: install pyright
5050
run: npm install -g pyright
5151
- name: Lint
52-
run: make lint
52+
run: make lint semantic_lint
5353

5454
test:
5555
runs-on: ${{ matrix.os }}
5656
timeout-minutes: 20
5757
strategy:
58-
max-parallel: 10
58+
max-parallel: 18
5959
matrix:
60-
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
60+
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
6161
os: [ubuntu-latest , macos-13, macos-14]
6262
exclude:
6363
- python-version: "3.8"
@@ -73,22 +73,27 @@ jobs:
7373
uses: actions/setup-python@v5
7474
with:
7575
python-version: ${{ matrix.python-version }}
76+
allow-prereleases: true
7677
- name: Set up Rust
7778
uses: dtolnay/rust-toolchain@stable
7879
with:
7980
toolchain: nightly
80-
- name: Install dependencies
81+
- name: Install components
8182
run: |
8283
python -m pip install --upgrade pip
83-
pip install -e .[dev,mixin]
8484
rustup component add clippy
85-
make dev
85+
- name: Test unit
86+
# ignore the mixin test for Python 3.13 since some of the dependencies are not ready
87+
if: ${{ startsWith(matrix.python-version, '3.13')}}
88+
run: |
89+
make test_unit
8690
- name: Test
91+
if: ${{ !startsWith(matrix.python-version, '3.13')}}
8792
run: |
88-
make semantic_lint test
93+
make test
8994
- name: Test shm in Linux
9095
# ignore the shm test for Python 3.12 since pyarrow doesn't have py3.12 wheel with version < 12
91-
if: ${{ startsWith(matrix.os, 'ubuntu') && !startsWith(matrix.python-version, '3.12') }}
96+
if: ${{ startsWith(matrix.os, 'ubuntu') && !startsWith(matrix.python-version, '3.12') && !startsWith(matrix.python-version, '3.13') }}
9297
run: |
9398
sudo apt update && sudo apt install redis
9499
make test_shm

Makefile

+8-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,19 @@ dev:
1313
cargo build
1414
@mkdir -p mosec/bin
1515
@cp ./target/debug/mosec mosec/bin/mosec
16-
pip install -e .
16+
pip install -e .[dev]
1717

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

24+
test_unit: dev
25+
echo "Running tests for the main logic"
26+
pytest -vv -s tests/test_log.py tests/test_protocol.py tests/test_coordinator.py
27+
RUST_BACKTRACE=1 cargo test -vv
28+
2329
test_shm: dev
2430
@pip install -q -r requirements/mixin.txt
2531
echo "Running tests for the shm mixin"

mosec/log.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import json
2020
import logging
2121
import os
22-
from datetime import datetime
22+
from datetime import datetime, timezone
2323
from typing import Any, MutableMapping
2424

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

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

4848

mosec/runtime.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,23 @@
1616

1717
import multiprocessing as mp
1818
import subprocess
19+
import sys
1920
from multiprocessing.context import ForkContext, SpawnContext
2021
from multiprocessing.process import BaseProcess
2122
from multiprocessing.synchronize import Event
2223
from pathlib import Path
2324
from time import monotonic, sleep
2425
from typing import Callable, Dict, Iterable, List, Optional, Type, Union, cast
2526

26-
import pkg_resources
27+
if sys.version_info >= (3, 9):
28+
from importlib.resources import files as importlib_files
29+
else:
30+
from pkg_resources import resource_filename
31+
32+
def importlib_files(package: str) -> Path:
33+
"""Get the resource file path."""
34+
return Path(resource_filename(package, ""))
35+
2736

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

242-
self.server_path = Path(
243-
pkg_resources.resource_filename("mosec", "bin"), "mosec"
244-
)
251+
self.server_path = importlib_files("mosec") / "bin" / "mosec"
245252
self.timeout = timeout
246253

247254
def halt(self):

pyproject.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ license = {text = "Apache-2.0"}
1111
keywords = ["machine learning", "deep learning", "model serving"]
1212
dynamic = ["version"]
1313
requires-python = ">=3.8"
14+
dependencies = []
1415
classifiers = [
1516
"Environment :: GPU",
1617
"Intended Audience :: Developers",
@@ -80,8 +81,6 @@ markers = [
8081
"shm: mark a test is related to shared memory",
8182
]
8283

83-
[tool.ruff]
84-
target-version = "py38"
8584
[tool.ruff.lint]
8685
select = ["E", "F", "G", "B", "I", "SIM", "TID", "PL", "RUF", "D"]
8786
ignore = ["E501", "D203", "D213"]

requirements/dev.txt

-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,5 @@ mypy~=1.11
55
pyright~=1.1
66
ruff~=0.6
77
pre-commit>=2.15.0
8-
msgpack>=1.0.5
9-
numpy<2
108
httpx==0.27.2
119
httpx-sse==0.4.0

requirements/mixin.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ msgspec>=0.15.0
33
numbin>=0.5.0
44
pyarrow>=0.6.1,<12; python_version < "3.12"
55
redis>=4.0.0
6+
numpy<2 # pyarrow legacy dependency

tests/test_coordinator.py

-14
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@
2929
from os.path import join
3030
from typing import Union, cast
3131

32-
import msgpack # type: ignore
3332
import pytest
3433

3534
from mosec.coordinator import PROTOCOL_TIMEOUT, Coordinator, State
36-
from mosec.mixin import MsgpackMixin
3735
from mosec.protocol import HTTPStatusCode, _recv_all
3836
from mosec.worker import Worker
3937
from tests.utils import imitate_controller_send
@@ -65,10 +63,6 @@ def forward(self, data):
6563
return data
6664

6765

68-
class EchoWorkerMsgPack(MsgpackMixin, EchoWorkerJSON):
69-
pass
70-
71-
7266
@pytest.fixture
7367
def base_test_config():
7468
return {
@@ -192,14 +186,6 @@ def test_incorrect_socket_file(mocker, base_test_config, caplog):
192186
EchoWorkerJSON,
193187
json.loads,
194188
),
195-
(
196-
[
197-
msgpack.packb({"rid": "147982364", "data": b"im_bytes"}),
198-
msgpack.packb({"rid": "147982831", "data": b"another_im_bytes"}),
199-
],
200-
EchoWorkerMsgPack,
201-
msgpack.unpackb,
202-
),
203189
],
204190
)
205191
def test_echo_batch(base_test_config, test_data, worker, deserializer):

0 commit comments

Comments
 (0)