Debian Codegraph @

testing/web-platform/tests/tools/third_party/websockets/example/faq/shutdown_server.py

raw
1 #!/usr/bin/env python
2
3 import asyncio
4 import signal
5 import websockets
6
7 async def echo(websocket):
8 async for message in websocket:
9 await websocket.send(message)
10
11 async def server():
12 # Set the stop condition when receiving SIGTERM.
13 loop = asyncio.get_running_loop()
14 stop = loop.create_future()
15 loop.add_signal_handler(signal.SIGTERM, stop.set_result, None)
16
17 async with websockets.serve(echo, "localhost", 8765):
18 await stop
19
20 asyncio.run(server())