make waiting for the server to start robust

This commit is contained in:
Benjamin Peterson 2010-02-06 22:08:15 +00:00
parent 26a1f72637
commit 239f138aea
2 changed files with 19 additions and 11 deletions

View File

@ -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():
"""

View File

@ -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)