Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from fastapi.responses import HTMLResponse
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
-
import
|
| 5 |
import json
|
| 6 |
|
| 7 |
app = FastAPI()
|
|
@@ -16,27 +16,32 @@ async def root():
|
|
| 16 |
@app.get("/api/benchmark")
|
| 17 |
async def run_benchmark(orders: int = 1000000, cancel: float = 0.20, market: float = 0.10):
|
| 18 |
cmd = [
|
| 19 |
-
"./build/benchmark",
|
| 20 |
-
"--orders", str(orders),
|
| 21 |
-
"--cancel-ratio", str(cancel),
|
| 22 |
-
"--market-ratio", str(market),
|
| 23 |
-
"--json",
|
| 24 |
"--quiet"
|
| 25 |
]
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
try:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
return json.loads(stdout.decode())
|
| 39 |
except json.JSONDecodeError:
|
| 40 |
-
return {"error": f"Invalid JSON output from C++: {
|
| 41 |
except Exception as e:
|
| 42 |
return {"error": f"Server execution error: {str(e)}"}
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from fastapi.responses import HTMLResponse
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
+
import subprocess
|
| 5 |
import json
|
| 6 |
|
| 7 |
app = FastAPI()
|
|
|
|
| 16 |
@app.get("/api/benchmark")
|
| 17 |
async def run_benchmark(orders: int = 1000000, cancel: float = 0.20, market: float = 0.10):
|
| 18 |
cmd = [
|
| 19 |
+
"./build/benchmark",
|
| 20 |
+
"--orders", str(orders),
|
| 21 |
+
"--cancel-ratio", str(cancel),
|
| 22 |
+
"--market-ratio", str(market),
|
| 23 |
+
"--json",
|
| 24 |
"--quiet"
|
| 25 |
]
|
| 26 |
+
|
| 27 |
+
# BUG FIX: Original code had `result` referenced in the JSONDecodeError
|
| 28 |
+
# handler, but `result` is only assigned inside the try block when
|
| 29 |
+
# check=True raises CalledProcessError before the assignment can happen.
|
| 30 |
+
# This would throw a NameError, masking the real error. Fix: capture
|
| 31 |
+
# stdout explicitly before calling check, so result is always defined.
|
| 32 |
+
stdout_data = ""
|
| 33 |
try:
|
| 34 |
+
proc = subprocess.run(cmd, capture_output=True, text=True)
|
| 35 |
+
stdout_data = proc.stdout
|
| 36 |
+
|
| 37 |
+
if proc.returncode != 0:
|
| 38 |
+
return {
|
| 39 |
+
"error": f"C++ Binary Crashed (Code {proc.returncode}). Stderr: {proc.stderr}"
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
return json.loads(stdout_data)
|
| 43 |
+
|
|
|
|
| 44 |
except json.JSONDecodeError:
|
| 45 |
+
return {"error": f"Invalid JSON output from C++: {stdout_data!r}"}
|
| 46 |
except Exception as e:
|
| 47 |
return {"error": f"Server execution error: {str(e)}"}
|