Skip to content

Commit 603dd98

Browse files
committed
Document how to wait for clients to disconnect.
Fix #1756.
1 parent f78b0b3 commit 603dd98

3 files changed

Lines changed: 34 additions & 7 deletions

File tree

‎src/websockets/asyncio/server.py‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -552,16 +552,31 @@ async def handler(websocket):
552552
await stop.wait()
553553
554554
Alternatively, await it and call :meth:`~Server.serve_forever` to serve
555-
requests, then cancel it or call :meth:`~Server.close` to stop the server::
555+
requests, then cancel it to stop the server::
556556
557557
server = await serve(handler, host, port)
558558
await server.serve_forever()
559559
560-
The following pattern is functional but redundant: by the time the context
560+
Combining these two patterns works, but is unnecessary: when the context
561561
manager exits, :meth:`~Server.serve_forever` has already closed the server::
562562
563-
async with serve(handler, host, port) as server:
564-
await server.serve_forever()
563+
async with serve(handler, host, port) as server: # don't do this!
564+
await server.serve_forever() # it's redundant
565+
566+
You can also stop the server gracefully by calling its
567+
:meth:`~Server.close` method::
568+
569+
server = await serve(handler, host, port)
570+
try:
571+
await stop.wait()
572+
finally:
573+
server.close()
574+
await server.wait_closed()
575+
576+
By default, closing the server closes connections with code 1001 (going
577+
away). You can keep connections alive and wait for clients to disconnect::
578+
579+
server.close(close_connections=False)
565580
566581
Args:
567582
handler: Connection handler. It receives the WebSocket connection,

‎src/websockets/sync/server.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,8 +544,15 @@ def handler(websocket):
544544
with serve(handler, ...) as server:
545545
server.serve_forever()
546546
547-
To stop the server gracefully, call its :meth:`~Server.shutdown` method from
548-
another thread.
547+
You can stop the server gracefully by calling its :meth:`~Server.shutdown`
548+
method from another thread::
549+
550+
server.shutdown()
551+
552+
By default, closing the server closes connections with code 1001 (going
553+
away). You can keep connections alive and wait for clients to disconnect::
554+
555+
server.shutdown(close_connections=False)
549556
550557
Args:
551558
handler: Connection handler. It receives the WebSocket connection,

‎src/websockets/trio/server.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ async def handler(websocket):
439439
async with server:
440440
await stop.wait()
441441
442-
Alternatively, to stop the server gracefully, call its
442+
Alternatively, you can stop the server gracefully by calling its
443443
:meth:`~Server.aclose` method::
444444
445445
with trio.open_nursery() as nursery:
@@ -449,6 +449,11 @@ async def handler(websocket):
449449
finally:
450450
await server.aclose()
451451
452+
By default, closing the server closes connections with code 1001 (going
453+
away). You can keep connections alive and wait for clients to disconnect::
454+
455+
await server.aclose(close_connections=False)
456+
452457
Args:
453458
handler: Connection handler. It receives the WebSocket connection,
454459
which is a :class:`ServerConnection`, in argument.

0 commit comments

Comments
 (0)