@@ -4,9 +4,39 @@ Deployment
4
4
The author of ``websockets `` isn't aware of best practices for deploying
5
5
network services based on :mod: `asyncio `.
6
6
7
- He suggests running a Python script similar to the :ref: `server example
8
- <server-example>`, perhaps inside a supervisor if you deem it useful.
7
+ You can run a script similar to the :ref: `server example <server-example >`,
8
+ inside a supervisor if you deem that useful.
9
+
10
+ You can also add a wrapper to daemonize the process. Third-party libraries
11
+ provide solutions for that.
9
12
10
13
If you can share knowledge on this topic, please file an issue _. Thanks!
11
14
12
15
.. _issue : https://github.com/aaugustin/websockets/issues/new
16
+
17
+ Graceful shutdown
18
+ -----------------
19
+
20
+ You may want to close connections gracefully when shutting down the server,
21
+ perhaps after executing some cleanup logic.
22
+
23
+ The proper way to do this is to call the ``close() `` method of the object
24
+ returned by :func: `~websockets.server.serve `, then wait for ``wait_closed() ``
25
+ to complete.
26
+
27
+ Tasks that handle connections will be cancelled, in the sense that
28
+ :meth: `~websockets.protocol.WebSocketCommonProtocol.recv ` raises
29
+ :exc: `~asyncio.CancelledError `.
30
+
31
+ On Unix systems, shutdown is usually triggered by sending a signal.
32
+
33
+ Here's a full example (Unix-only):
34
+
35
+ .. literalinclude :: ../example/shutdown.py
36
+
37
+
38
+ It's more difficult to achieve the same effect on Windows. Some third-party
39
+ projects try to help with this problem.
40
+
41
+ If your server doesn't run in the main thread, look at
42
+ :func: `~asyncio.AbstractEventLoop.call_soon_threadsafe `.
0 commit comments