From 239f138aea2812dc948a46e376f3533efb70e10a Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Sat, 6 Feb 2010 22:08:15 +0000 Subject: [PATCH] make waiting for the server to start robust --- Lib/logging/config.py | 28 ++++++++++++++++++---------- Lib/test/test_logging.py | 2 +- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/Lib/logging/config.py b/Lib/logging/config.py index d7f7481fe80..1145e711903 100644 --- a/Lib/logging/config.py +++ b/Lib/logging/config.py @@ -843,17 +843,25 @@ def listen(port=DEFAULT_LOGGING_CONFIG_PORT): abort = self.abort logging._releaseLock() - def serve(rcvr, hdlr, port): - server = rcvr(port=port, handler=hdlr) - global _listener - logging._acquireLock() - _listener = server - logging._releaseLock() - server.serve_until_stopped() + class Server(threading.Thread): - return threading.Thread(target=serve, - args=(ConfigSocketReceiver, - ConfigStreamHandler, port)) + def __init__(self, rcvr, hdlr, port): + super(Server, self).__init__() + self.rcvr = rcvr + self.hdlr = hdlr + self.port = port + self.ready = threading.Event() + + def run(self): + server = self.rcvr(port=self.port, handler=self.hdlr) + self.ready.set() + global _listener + logging._acquireLock() + _listener = server + logging._releaseLock() + server.serve_until_stopped() + + return Server(ConfigSocketReceiver, ConfigStreamHandler, port) def stopListening(): """ diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 2a5283cadfb..b9825cd5a4b 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -1574,7 +1574,7 @@ class ConfigDictTest(BaseTest): port = find_unused_port() t = logging.config.listen(port) t.start() - time.sleep(0.5) # give server thread some time to get ready + t.ready.wait() try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.settimeout(2.0)