MCPcopy Index your code
hub / github.com/python-websockets/websockets / serve_forever

Method serve_forever

src/websockets/sync/server.py:250–286  ·  view source on GitHub ↗

See :meth:`socketserver.BaseServer.serve_forever`. This method doesn't return. Calling :meth:`shutdown` from another thread stops the server. Typical use:: with serve(...) as server: server.serve_forever()

(self)

Source from the content-addressed store, hash-verified

248 self.shutdown_watcher, self.shutdown_notifier = os.pipe()
249
250 def serve_forever(self) -> None:
251 """
252 See :meth:`socketserver.BaseServer.serve_forever`.
253
254 This method doesn't return. Calling :meth:`shutdown` from another thread
255 stops the server.
256
257 Typical use::
258
259 with serve(...) as server:
260 server.serve_forever()
261
262 """
263 poller = selectors.DefaultSelector()
264 try:
265 poller.register(self.socket, selectors.EVENT_READ)
266 except ValueError: # pragma: no cover
267 # If shutdown() is called before poller.register(),
268 # the socket is closed and poller.register() raises
269 # ValueError: Invalid file descriptor: -1
270 return
271 if sys.platform != "win32":
272 poller.register(self.shutdown_watcher, selectors.EVENT_READ)
273
274 while True:
275 poller.select()
276 try:
277 # If the socket is closed, this will raise an exception and exit
278 # the loop. So we don't need to check the return value of select().
279 sock, addr = self.socket.accept()
280 except OSError:
281 break
282 # Since there isn't a mechanism for tracking connections and waiting
283 # for them to terminate, we cannot use daemon threads, or else all
284 # connections would be terminate brutally when closing the server.
285 thread = threading.Thread(target=self.handler, args=(sock, addr))
286 thread.start()
287
288 def shutdown(self) -> None:
289 """

Callers 15

mainFunction · 0.45
mainFunction · 0.45
mainFunction · 0.45
mainFunction · 0.45
mainFunction · 0.45
mainFunction · 0.45
mainFunction · 0.45
mainFunction · 0.45
mainFunction · 0.45
mainFunction · 0.45
mainFunction · 0.45
mainFunction · 0.45

Calls 2

registerMethod · 0.80
acceptMethod · 0.80

Tested by

no test coverage detected