diff --git a/main.py b/main.py index 523e88f..4338b70 100644 --- a/main.py +++ b/main.py @@ -35,6 +35,8 @@ if not os.path.exists(tmpDir): app = FastAPI() +lock_simulation = asyncio.Lock() + app.add_middleware(GZipMiddleware, minimum_size=1000) ############################################################ @@ -146,20 +148,18 @@ 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) async def fastapi_run_project(network: str) -> str: - print(app.state.lock_simulation) - if app.state.lock_simulation: + global lock_simulation + + if lock_simulation.locked: raise HTTPException(status_code=409, detail="is in simulation") - - app.state.lock_simulation = True - print('lock simulation') - result = run_project(network) - app.state.lock_simulation = False - return result + + async with lock_simulation: + print('lock simulation') + result = run_project(network) + return result # put in inp folder, name without extension @app.get("/runinp/")