Fix: clean up run.py newlines and indentation; add optional RUN_SECONDS timeout helper for server tasks
This commit is contained in:
30
run.py
30
run.py
@@ -1,4 +1,4 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
@@ -10,7 +10,8 @@ async def run_in_memory():
|
|||||||
from server.transport import InMemoryTransport
|
from server.transport import InMemoryTransport
|
||||||
cfg = ServerConfig()
|
cfg = ServerConfig()
|
||||||
server = GameServer(transport=InMemoryTransport(lambda d, p: server.on_datagram(d, p)), config=cfg)
|
server = GameServer(transport=InMemoryTransport(lambda d, p: server.on_datagram(d, p)), config=cfg)
|
||||||
await asyncio.gather(server.transport.run(), server.tick_loop())
|
tasks = [asyncio.create_task(server.transport.run()), asyncio.create_task(server.tick_loop())]
|
||||||
|
await _run_tasks_with_optional_timeout(tasks)
|
||||||
|
|
||||||
|
|
||||||
async def run_quic():
|
async def run_quic():
|
||||||
@@ -21,7 +22,8 @@ async def run_quic():
|
|||||||
cert = os.environ["QUIC_CERT"]
|
cert = os.environ["QUIC_CERT"]
|
||||||
key = os.environ["QUIC_KEY"]
|
key = os.environ["QUIC_KEY"]
|
||||||
server = GameServer(transport=QuicWebTransportServer(host, port, cert, key, lambda d, p: server.on_datagram(d, p)), config=cfg)
|
server = GameServer(transport=QuicWebTransportServer(host, port, cert, key, lambda d, p: server.on_datagram(d, p)), config=cfg)
|
||||||
await asyncio.gather(server.transport.run(), server.tick_loop())
|
tasks = [asyncio.create_task(server.transport.run()), asyncio.create_task(server.tick_loop())]
|
||||||
|
await _run_tasks_with_optional_timeout(tasks)
|
||||||
|
|
||||||
|
|
||||||
async def run_webtransport():
|
async def run_webtransport():
|
||||||
@@ -44,7 +46,8 @@ async def run_webtransport():
|
|||||||
server = GameServer(transport=WebTransportServer(host, port, cert, key, lambda d, p: server.on_datagram(d, p)), config=cfg)
|
server = GameServer(transport=WebTransportServer(host, port, cert, key, lambda d, p: server.on_datagram(d, p)), config=cfg)
|
||||||
print(f"WebTransport server: https://{host}:{port}/ (HTTP/3)")
|
print(f"WebTransport server: https://{host}:{port}/ (HTTP/3)")
|
||||||
try:
|
try:
|
||||||
await asyncio.gather(server.transport.run(), server.tick_loop())
|
tasks = [asyncio.create_task(server.transport.run()), asyncio.create_task(server.tick_loop())]
|
||||||
|
await _run_tasks_with_optional_timeout(tasks)
|
||||||
finally:
|
finally:
|
||||||
if httpd is not None:
|
if httpd is not None:
|
||||||
httpd.shutdown()
|
httpd.shutdown()
|
||||||
@@ -67,3 +70,22 @@ if __name__ == "__main__":
|
|||||||
asyncio.run(run_in_memory())
|
asyncio.run(run_in_memory())
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
async def _run_tasks_with_optional_timeout(tasks):
|
||||||
|
"""Await tasks, optionally honoring RUN_SECONDS env var to cancel after a timeout."""
|
||||||
|
timeout_s = os.environ.get("RUN_SECONDS")
|
||||||
|
if not timeout_s:
|
||||||
|
await asyncio.gather(*tasks)
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
await asyncio.wait_for(asyncio.gather(*tasks), timeout=float(timeout_s))
|
||||||
|
except asyncio.TimeoutError:
|
||||||
|
logging.info("Timeout reached (RUN_SECONDS=%s); stopping server tasks...", timeout_s)
|
||||||
|
for t in tasks:
|
||||||
|
t.cancel()
|
||||||
|
await asyncio.gather(*tasks, return_exceptions=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user