Update app.py
Browse filesaddition of get_container_cpu_count()
app.py
CHANGED
|
@@ -5,9 +5,28 @@ from fastapi import FastAPI
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
@app.get("/")
|
| 9 |
def greet_json():
|
| 10 |
-
return {"Hello": "World!", "cpu_count":
|
| 11 |
|
| 12 |
@app.get("/health")
|
| 13 |
def health():
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
+
def get_container_cpu_count():
|
| 9 |
+
try:
|
| 10 |
+
# default CPU count from Python
|
| 11 |
+
cpu_count = os.cpu_count()
|
| 12 |
+
|
| 13 |
+
# read cgroup quota (if available)
|
| 14 |
+
quota_path = "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"
|
| 15 |
+
period_path = "/sys/fs/cgroup/cpu/cpu.cfs_period_us"
|
| 16 |
+
if os.path.exists(quota_path) and os.path.exists(period_path):
|
| 17 |
+
with open(quota_path) as f:
|
| 18 |
+
quota = int(f.read())
|
| 19 |
+
with open(period_path) as f:
|
| 20 |
+
period = int(f.read())
|
| 21 |
+
if quota > 0 and period > 0:
|
| 22 |
+
cpu_count = quota / period
|
| 23 |
+
return int(cpu_count)
|
| 24 |
+
except Exception:
|
| 25 |
+
return os.cpu_count()
|
| 26 |
+
|
| 27 |
@app.get("/")
|
| 28 |
def greet_json():
|
| 29 |
+
return {"Hello": "World!", "cpu_count": get_container_cpu_count(), "total_ram_in_MB": psutil.virtual_memory().total // (1024*1024)}
|
| 30 |
|
| 31 |
@app.get("/health")
|
| 32 |
def health():
|