-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglyph_http_server.py
More file actions
76 lines (57 loc) · 1.61 KB
/
Copy pathglyph_http_server.py
File metadata and controls
76 lines (57 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
import asyncio
import json
import subprocess
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
app = FastAPI()
proc = None
lock = asyncio.Lock()
class Query(BaseModel):
hex: str
@app.on_event("startup")
async def startup():
global proc
proc = subprocess.Popen(
[
"./glyph_segmented_live.py",
"--config", "config/shards_8gb_demo.json",
"--server-bin", "build/query_fm_server_v1"
],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
bufsize=1,
)
# ждём READY
while True:
line = proc.stderr.readline().strip()
if line.startswith("READY"):
break
@app.on_event("shutdown")
async def shutdown():
global proc
if proc:
proc.kill()
@app.get("/health")
async def health():
if proc is None or proc.poll() is not None:
raise HTTPException(status_code=500, detail="engine down")
return {"status": "ok"}
@app.post("/query")
async def query(q: Query):
global proc
if proc is None or proc.poll() is not None:
raise HTTPException(status_code=500, detail="engine down")
async with lock:
try:
proc.stdin.write("HEX " + q.hex + "\n")
proc.stdin.flush()
line = await asyncio.wait_for(
asyncio.to_thread(proc.stdout.readline),
timeout=5.0
)
return json.loads(line.strip())
except asyncio.TimeoutError:
raise HTTPException(status_code=504, detail="timeout")