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