Refine
This commit is contained in:
12
main.py
12
main.py
@@ -146,20 +146,22 @@ async def fastapi_dump_inp(network: str, inp: str) -> bool:
|
||||
dump_inp(network, inp)
|
||||
return True
|
||||
|
||||
lock = threading.Lock()
|
||||
app.state.lock = asyncio.Lock() # Create an async lock object
|
||||
|
||||
# 必须用这个PlainTextResponse,不然每个key都有引号
|
||||
@app.get("/runproject/", response_class = PlainTextResponse)
|
||||
async def fastapi_run_project(network: str) -> str:
|
||||
if lock.locked():
|
||||
if app.state.lock.locked():
|
||||
raise HTTPException(status_code=400, detail="is in simulation")
|
||||
else:
|
||||
try:
|
||||
with lock:
|
||||
result = run_project(network)
|
||||
return result
|
||||
app.state.lock.acquire() # Acquire the lock
|
||||
result = run_project(network)
|
||||
return result
|
||||
except asyncio.CancelledError:
|
||||
raise HTTPException(status_code=400, detail="is in simulation")
|
||||
finally:
|
||||
app.state.lock.release()
|
||||
|
||||
# put in inp folder, name without extension
|
||||
@app.get("/runinp/")
|
||||
|
||||
Reference in New Issue
Block a user