Fix server shutdown

This commit is contained in:
kngwyu 2021-04-14 12:06:56 +09:00
parent 15a1a1384f
commit a2ba3511cd
3 changed files with 6 additions and 5 deletions

View File

@ -1 +1,2 @@
include mujoco-maze/static/index.html include mujoco-maze/static/favicon.ico
include mujoco-maze/templates/index.html

View File

@ -370,7 +370,6 @@ class MazeEnv(gym.Env):
super().__init__() super().__init__()
self.pipe = pipe self.pipe = pipe
self.port = port self.port = port
self.server = None
def _run_server(self) -> None: def _run_server(self) -> None:
import asyncio import asyncio
@ -384,6 +383,7 @@ class MazeEnv(gym.Env):
app = fastapi.FastAPI() app = fastapi.FastAPI()
html_path = pathlib.Path(__file__).parent.joinpath("static/index.html") html_path = pathlib.Path(__file__).parent.joinpath("static/index.html")
html = html_path.read_text().replace("{{port}}", str(self.port)) html = html_path.read_text().replace("{{port}}", str(self.port))
server = None
@app.get("/") @app.get("/")
async def get(): async def get():
@ -403,11 +403,11 @@ class MazeEnv(gym.Env):
res = stream.getvalue() res = stream.getvalue()
await websocket.send_bytes(res) await websocket.send_bytes(res)
await websocket.close() await websocket.close()
await self.server.shutdown() server.should_exit = True
config = uvicorn.Config(app, port=self.port) config = uvicorn.Config(app, port=self.port)
self.server = uvicorn.Server(config) server = uvicorn.Server(config)
self.server.run() server.run()
def run(self) -> None: def run(self) -> None:
try: try:

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB