@@ -180,6 +180,7 @@ class LanguageTool:
180180
181181 _port = _MIN_PORT
182182 _server = None
183+ _consumer_thread = None
183184 _instances = WeakValueDictionary ()
184185 _PORT_RE = re .compile (r"(?:https?://.*:|port\s+)(\d+)" , re .I )
185186
@@ -361,19 +362,18 @@ def _start_server(cls):
361362 if port != cls ._port :
362363 raise Error (err_msg )
363364
364- def consume ():
365- """Consume/ignore the rest of the server output.
366-
367- Without this, the server will end up hanging due to the buffer
368- filling up.
369-
370- """
371- while cls ._server and cls ._server .stdout .readline ():
372- pass
373- cls ._thread = threading .Thread (target = consume )
374- cls ._thread .daemon = True
375- cls ._thread .start ()
376- if not cls ._server :
365+ if cls ._server :
366+ if cls ._consumer_thread :
367+ # This means there was a previous server/thread pair. Since the
368+ # old server is gone, the thread must be finished consuming
369+ # too.
370+ cls ._consumer_thread .join ()
371+ cls ._consumer_thread = None
372+ cls ._consumer_thread = threading .Thread (
373+ target = lambda : _consume (cls ._server .stdout ))
374+ cls ._consumer_thread .daemon = True
375+ cls ._consumer_thread .start ()
376+ else :
377377 # Couldn't start the server, so maybe there is already one running.
378378 params = {'language' : FAILSAFE_LANGUAGE , 'text' : '' }
379379 data = urllib .parse .urlencode (params ).encode ()
@@ -403,6 +403,17 @@ def _terminate_server(cls):
403403 pass
404404
405405
406+ def _consume (stdout ):
407+ """Consume/ignore the rest of the server output.
408+
409+ Without this, the server will end up hanging due to the buffer
410+ filling up.
411+
412+ """
413+ while stdout .readline ():
414+ pass
415+
416+
406417@total_ordering
407418class LanguageTag (str ):
408419
0 commit comments