diff --git a/poetry.lock b/poetry.lock index 199554f..245a57f 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1198,6 +1198,35 @@ files = [ dev = ["pre-commit", "tox"] testing = ["pytest", "pytest-benchmark"] +[[package]] +name = "psutil" +version = "6.0.0" +description = "Cross-platform lib for process and system monitoring in Python." +optional = false +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*,!=3.5.*,>=2.7" +files = [ + {file = "psutil-6.0.0-cp27-cp27m-macosx_10_9_x86_64.whl", hash = "sha256:a021da3e881cd935e64a3d0a20983bda0bb4cf80e4f74fa9bfcb1bc5785360c6"}, + {file = "psutil-6.0.0-cp27-cp27m-manylinux2010_i686.whl", hash = "sha256:1287c2b95f1c0a364d23bc6f2ea2365a8d4d9b726a3be7294296ff7ba97c17f0"}, + {file = "psutil-6.0.0-cp27-cp27m-manylinux2010_x86_64.whl", hash = "sha256:a9a3dbfb4de4f18174528d87cc352d1f788b7496991cca33c6996f40c9e3c92c"}, + {file = "psutil-6.0.0-cp27-cp27mu-manylinux2010_i686.whl", hash = "sha256:6ec7588fb3ddaec7344a825afe298db83fe01bfaaab39155fa84cf1c0d6b13c3"}, + {file = "psutil-6.0.0-cp27-cp27mu-manylinux2010_x86_64.whl", hash = "sha256:1e7c870afcb7d91fdea2b37c24aeb08f98b6d67257a5cb0a8bc3ac68d0f1a68c"}, + {file = "psutil-6.0.0-cp27-none-win32.whl", hash = "sha256:02b69001f44cc73c1c5279d02b30a817e339ceb258ad75997325e0e6169d8b35"}, + {file = "psutil-6.0.0-cp27-none-win_amd64.whl", hash = "sha256:21f1fb635deccd510f69f485b87433460a603919b45e2a324ad65b0cc74f8fb1"}, + {file = "psutil-6.0.0-cp36-abi3-macosx_10_9_x86_64.whl", hash = "sha256:c588a7e9b1173b6e866756dde596fd4cad94f9399daf99ad8c3258b3cb2b47a0"}, + {file = "psutil-6.0.0-cp36-abi3-manylinux_2_12_i686.manylinux2010_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:6ed2440ada7ef7d0d608f20ad89a04ec47d2d3ab7190896cd62ca5fc4fe08bf0"}, + {file = "psutil-6.0.0-cp36-abi3-manylinux_2_12_x86_64.manylinux2010_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:5fd9a97c8e94059b0ef54a7d4baf13b405011176c3b6ff257c247cae0d560ecd"}, + {file = "psutil-6.0.0-cp36-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e2e8d0054fc88153ca0544f5c4d554d42e33df2e009c4ff42284ac9ebdef4132"}, + {file = "psutil-6.0.0-cp36-cp36m-win32.whl", hash = "sha256:fc8c9510cde0146432bbdb433322861ee8c3efbf8589865c8bf8d21cb30c4d14"}, + {file = "psutil-6.0.0-cp36-cp36m-win_amd64.whl", hash = "sha256:34859b8d8f423b86e4385ff3665d3f4d94be3cdf48221fbe476e883514fdb71c"}, + {file = "psutil-6.0.0-cp37-abi3-win32.whl", hash = "sha256:a495580d6bae27291324fe60cea0b5a7c23fa36a7cd35035a16d93bdcf076b9d"}, + {file = "psutil-6.0.0-cp37-abi3-win_amd64.whl", hash = "sha256:33ea5e1c975250a720b3a6609c490db40dae5d83a4eb315170c4fe0d8b1f34b3"}, + {file = "psutil-6.0.0-cp38-abi3-macosx_11_0_arm64.whl", hash = "sha256:ffe7fc9b6b36beadc8c322f84e1caff51e8703b88eee1da46d1e3a6ae11b4fd0"}, + {file = "psutil-6.0.0.tar.gz", hash = "sha256:8faae4f310b6d969fa26ca0545338b21f73c6b15db7c4a8d934a5482faa818f2"}, +] + +[package.extras] +test = ["enum34", "ipaddress", "mock", "pywin32", "wmi"] + [[package]] name = "pydantic" version = "2.8.2" @@ -2153,4 +2182,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.12,<3.13" -content-hash = "0268008a0ef4c60db0944a20a409f6ea4d97cb10fea83a68f9e8a329bece3222" +content-hash = "d25c449a44826867c43c7856d6ff5e6cf9ff6b253588dc6cef42a824dadbe5e2" diff --git a/pyproject.toml b/pyproject.toml index afcf4da..06f8cf2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "sagemaker-shim" -version = "0.3.3" +version = "0.3.4" description = "Adapts algorithms that implement the Grand Challenge inference API for running in SageMaker" authors = ["James Meakin <12661555+jmsmkn@users.noreply.github.com>"] license = "Apache-2.0" @@ -21,6 +21,7 @@ fastapi = "!=0.89.0" # See https://github.com/DIAGNijmegen/rse-sagemaker-shim/i uvicorn = "*" click = "*" boto3 = "*" +psutil = "*" [tool.poetry.group.dev.dependencies] pytest = "!=8.0.0" # pytest 8 is not yet supported by pytest-asyncio diff --git a/sagemaker_shim/cli.py b/sagemaker_shim/cli.py index 7ae223a..5c8e0ee 100644 --- a/sagemaker_shim/cli.py +++ b/sagemaker_shim/cli.py @@ -1,5 +1,7 @@ import asyncio import logging.config +import os +import resource import sys from collections.abc import Callable, Coroutine from functools import wraps @@ -7,6 +9,7 @@ from typing import Any, TypeVar import click +import psutil import uvicorn from botocore.exceptions import ClientError, NoCredentialsError from pydantic import ValidationError @@ -101,7 +104,24 @@ async def invoke(tasks: str, file: str) -> None: raise click.UsageError("Empty task list provided") +def set_memory_limits() -> None: + reserved_bytes = int( + os.environ.get( + "GRAND_CHALLENGE_COMPONENT_RESERVED_BYTES", 1_073_741_824 + ) + ) + + if reserved_bytes: + total_memory_bytes = psutil.virtual_memory().total + + limit = total_memory_bytes - reserved_bytes + + resource.setrlimit(resource.RLIMIT_DATA, (limit, limit)) + + if __name__ == "__main__": + set_memory_limits() + # https://pyinstaller.org/en/stable/runtime-information.html#run-time-information we_are_bundled = getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS")