diff --git a/main.py b/main.py index c78e1b2..3e38de1 100644 --- a/main.py +++ b/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/")