Skip to content

Commit 63d3747

Browse files
committed
Update server.py with cross-platform memory usage stats using psutil #634
1 parent f12455a commit 63d3747

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

server/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ENV PYTHONUNBUFFERED=1
44
ENV CRYPTOGRAPHY_DONT_BUILD_RUST=1
55
RUN pip install --no-cache-dir --upgrade pip setuptools wheel
66
RUN pip install --no-cache-dir cryptography pycryptodome
7-
RUN pip3 install --no-cache-dir netifaces tinytuya
7+
RUN pip install --no-cache-dir netifaces tinytuya psutil colorama requests
88
COPY . .
99
CMD ["python3", "server.py"]
1010
EXPOSE 8888

server/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,12 @@ The UI at http://localhost:8888 allows you to view and control the devices.
113113
114114
## Release Notes
115115
116+
### p15 - Cross-Platform Memory Stats
117+
118+
* Switched to using the `psutil` library for memory usage stats, making server.py fully cross-platform (Windows, macOS, Linux). Fixes issue #634.
119+
* Memory usage is now reported on all platforms if `psutil` is installed; if not, the field is set to None.
120+
* Added requirements to documentation and header: `pip install psutil tinytuya colorama requests`.
121+
116122
### p14 - Recovery Logic
117123
118124
* Add main loop logic to try to recover when exception occurs.

server/server.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
Date: June 11, 2023
88
For more information see https://github.com/jasonacox/tinytuya
99
10+
Requirements:
11+
pip install psutil tinytuya colorama requests
12+
1013
Description
1114
Server continually listens for Tuya UDP discovery packets and updates the database of devices
1215
and uses devices.json to determine metadata about devices.
@@ -43,7 +46,14 @@
4346
print("WARN: Unable to import requests library, Cloud functions will not work.")
4447
print("WARN: Check dependencies. See https://github.com/jasonacox/tinytuya/issues/377")
4548
print("WARN: Error: {}.".format(impErr.args[0]))
46-
import resource
49+
50+
# Memory usage reporting uses the 'psutil' library, which is cross-platform.
51+
# If 'psutil' is not available, memory stats will be set to None.
52+
try:
53+
import psutil
54+
HAS_PSUTIL = True
55+
except ImportError:
56+
HAS_PSUTIL = False
4757
import signal
4858
import sys
4959
import os
@@ -62,7 +72,7 @@
6272
import tinytuya
6373
from tinytuya import scanner
6474

65-
BUILD = "p14"
75+
BUILD = "p15"
6676

6777
# Defaults from Environment
6878
APIPORT = int(os.getenv("APIPORT", "8888"))
@@ -426,7 +436,12 @@ def do_GET(self):
426436
elif self.path == '/stats':
427437
# Give Internal Stats
428438
serverstats['ts'] = int(time.time())
429-
serverstats['mem'] = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
439+
# Report memory usage using psutil if available (cross-platform)
440+
if HAS_PSUTIL:
441+
process = psutil.Process(os.getpid())
442+
serverstats['mem'] = process.memory_info().rss // 1024 # Resident Set Size in KB
443+
else:
444+
serverstats['mem'] = None # psutil not available
430445
serverstats['cloudcreds'] = cloudcreds
431446
serverstats['cloudsync'] = cloudsync
432447
serverstats['cloudsyncdone'] = cloudsyncdone

0 commit comments

Comments
 (0)