NOT-OMEGA commited on
Commit
fba6719
·
verified ·
1 Parent(s): 2db76dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -33
app.py CHANGED
@@ -1,33 +1,43 @@
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()
8
-
9
- # Mount frontend directory for static assets
10
- app.mount("/frontend", StaticFiles(directory="frontend"), name="frontend")
11
-
12
- @app.get("/", response_class=HTMLResponse)
13
- async def root():
14
- with open("frontend/index.html", "r") as f:
15
- return f.read()
16
-
17
- @app.get("/api/benchmark")
18
- async def run_benchmark(orders: int = 1000000, cancel: float = 0.20, market: float = 0.10):
19
- cmd = [
20
- "./build/benchmark",
21
- "--orders", str(orders),
22
- "--cancel-ratio", str(cancel),
23
- "--market-ratio", str(market),
24
- "--json",
25
- "--quiet"
26
- ]
27
-
28
- try:
29
- # Executing the real C++ binary
30
- result = subprocess.run(cmd, capture_output=True, text=True, check=True)
31
- return json.loads(result.stdout)
32
- except Exception as e:
33
- return {"error": str(e)}
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from fastapi.responses import HTMLResponse
3
+ from fastapi.staticfiles import StaticFiles
4
+ import asyncio
5
+ import json
6
+
7
+ app = FastAPI()
8
+
9
+ app.mount("/frontend", StaticFiles(directory="frontend"), name="frontend")
10
+
11
+ @app.get("/", response_class=HTMLResponse)
12
+ async def root():
13
+ with open("frontend/index.html", "r") as f:
14
+ return f.read()
15
+
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
+ # BUG 4 FIXED: Using non-blocking asyncio subprocess
29
+ process = await asyncio.create_subprocess_exec(
30
+ *cmd,
31
+ stdout=asyncio.subprocess.PIPE,
32
+ stderr=asyncio.subprocess.PIPE
33
+ )
34
+ stdout, stderr = await process.communicate()
35
+
36
+ if process.returncode != 0:
37
+ return {"error": f"C++ Binary Crashed (Code {process.returncode}). Stderr: {stderr.decode()}"}
38
+
39
+ return json.loads(stdout.decode())
40
+ except json.JSONDecodeError:
41
+ return {"error": f"Invalid JSON output from C++: {stdout.decode()}"}
42
+ except Exception as e:
43
+ return {"error": f"Server execution error: {str(e)}"}