From e910374019d1176daea45de2414f3b71f180d067 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Fri, 13 Jan 2023 22:46:31 -0800 Subject: [PATCH] server: better python memory usage tracking --- docker/install-scrypted-dependencies-mac.sh | 2 +- docker/template/Dockerfile.full.header | 2 +- server/.vscode/launch.json | 2 +- server/python/plugin-remote.py | 13 +++++++++---- server/src/plugin/runtime/python-worker.ts | 1 - 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/docker/install-scrypted-dependencies-mac.sh b/docker/install-scrypted-dependencies-mac.sh index d09e57cb2..55dffaefa 100755 --- a/docker/install-scrypted-dependencies-mac.sh +++ b/docker/install-scrypted-dependencies-mac.sh @@ -69,7 +69,7 @@ then fi RUN python$PYTHON_VERSION -m pip install --upgrade pip -RUN python$PYTHON_VERSION -m pip install aiofiles debugpy typing_extensions typing opencv-python +RUN python$PYTHON_VERSION -m pip install aiofiles debugpy typing_extensions typing opencv-python psutil echo "Installing Scrypted Launch Agent..." diff --git a/docker/template/Dockerfile.full.header b/docker/template/Dockerfile.full.header index 851e3c522..2ce241627 100644 --- a/docker/template/Dockerfile.full.header +++ b/docker/template/Dockerfile.full.header @@ -59,7 +59,7 @@ RUN apt-get -y install \ # python pip RUN python3 -m pip install --upgrade pip -RUN python3 -m pip install aiofiles debugpy typing_extensions typing +RUN python3 -m pip install aiofiles debugpy typing_extensions typing psutil ################################################################ # End section generated from template/Dockerfile.full.header diff --git a/server/.vscode/launch.json b/server/.vscode/launch.json index 37b81f687..36a28c4d6 100644 --- a/server/.vscode/launch.json +++ b/server/.vscode/launch.json @@ -28,7 +28,7 @@ "${workspaceFolder}/**/*.js" ], "env": { - "SCRYPTED_PYTHON_PATH": "python3.9", + // "SCRYPTED_PYTHON_PATH": "python3.9", // "SCRYPTED_SHARED_WORKER": "true", // "SCRYPTED_DISABLE_AUTHENTICATION": "true", // "DEBUG": "*", diff --git a/server/python/plugin-remote.py b/server/python/plugin-remote.py index e1154df70..dcdd7a368 100644 --- a/server/python/plugin-remote.py +++ b/server/python/plugin-remote.py @@ -19,7 +19,7 @@ from asyncio.streams import StreamReader, StreamWriter from collections.abc import Mapping from io import StringIO from os import sys -from typing import Any, Dict, List, Optional, Set, Tuple +from typing import Any, List, Optional, Set, Tuple import aiofiles import scrypted_python.scrypted_sdk.types @@ -479,10 +479,15 @@ async def async_main(loop: AbstractEventLoop): def stats_runner(): ptime = round(time.process_time() * 1000000) try: - import resource - heapTotal = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss + import psutil + process = psutil.Process(os.getpid()) + heapTotal = process.memory_info().rss except: - heapTotal = 0 + try: + import resource + heapTotal = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss + except: + heapTotal = 0 stats = { 'type': 'stats', 'cpuUsage': { diff --git a/server/src/plugin/runtime/python-worker.ts b/server/src/plugin/runtime/python-worker.ts index 0fffaee28..da6b47782 100644 --- a/server/src/plugin/runtime/python-worker.ts +++ b/server/src/plugin/runtime/python-worker.ts @@ -2,7 +2,6 @@ import child_process from 'child_process'; import fs from "fs"; import os from "os"; import path from 'path'; -import readline from 'readline'; import { Readable, Writable } from 'stream'; import { RpcMessage, RpcPeer } from "../../rpc"; import { createRpcDuplexSerializer } from '../../rpc-serializer';