(
serve_or_route,
handler_or_url_map,
host="localhost",
port=0,
**kwargs,
)
| 38 | |
| 39 | @contextlib.contextmanager |
| 40 | def 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 | |
| 75 | def run_server(handler=handler, **kwargs): |
no test coverage detected
searching dependent graphs…