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