diff --git a/main.py b/main.py index 02b8f7a..ce94cb8 100644 --- a/main.py +++ b/main.py @@ -33,8 +33,6 @@ if not os.path.exists(inpDir): if not os.path.exists(tmpDir): os.mkdir(tmpDir) -lock_simulation = False - app = FastAPI() app.add_middleware(GZipMiddleware, minimum_size=1000) @@ -148,16 +146,17 @@ async def fastapi_dump_inp(network: str, inp: str) -> bool: dump_inp(network, inp) return True +app.state.lock_simulation = False + # 必须用这个PlainTextResponse,不然每个key都有引号 @app.get("/runproject/", response_class = PlainTextResponse) -def fastapi_run_project(network: str) -> str: - global lock_simulation - if lock_simulation: +async def fastapi_run_project(network: str) -> str: + if app.state.lock_simulation: raise HTTPException(status_code=409, detail="is in simulation") - lock_simulation = True + app.state.lock_simulation = True result = run_project(network) - lock_simulation = False + app.state.lock_simulation = False return result # put in inp folder, name without extension