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

Function run_server_or_router

tests/sync/server.py:40–72  ·  view source on GitHub ↗
(
    serve_or_route,
    handler_or_url_map,
    host="localhost",
    port=0,
    **kwargs,
)

Source from the content-addressed store, hash-verified

38
39@contextlib.contextmanager
40def run_server_or_router(
41 serve_or_route,
42 handler_or_url_map,
43 host="localhost",
44 port=0,
45 **kwargs,
46):
47 with serve_or_route(handler_or_url_map, host, port, **kwargs) as server:
48 thread = threading.Thread(target=server.serve_forever)
49 thread.start()
50
51 # HACK: since the sync server doesn't track connections (yet), we record
52 # a reference to the thread handling the most recent connection, then we
53 # can wait for that thread to terminate when exiting the context.
54 handler_thread = None
55 original_handler = server.handler
56
57 def handler(sock, addr):
58 nonlocal handler_thread
59 handler_thread = threading.current_thread()
60 original_handler(sock, addr)
61
62 server.handler = handler
63
64 try:
65 yield server
66 finally:
67 server.shutdown()
68 thread.join()
69
70 # HACK: wait for the thread handling the most recent connection.
71 if handler_thread is not None:
72 handler_thread.join()
73
74
75def run_server(handler=handler, **kwargs):

Callers 2

run_serverFunction · 0.85
run_routerFunction · 0.85

Calls 1

shutdownMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…