@@ -321,7 +321,8 @@ def _blocking_consume(self):
321321 if recover :
322322 recover = self ._stop_request_generator (request_generator )
323323 if not recover :
324- self .stop_consuming ()
324+ self ._stop_no_join ()
325+ return
325326
326327 def start_consuming (self ):
327328 """Start consuming the stream."""
@@ -336,11 +337,35 @@ def start_consuming(self):
336337 _LOGGER .debug ('Started helper thread %s' , thread .name )
337338 self ._consumer_thread = thread
338339
339- def stop_consuming (self ):
340- """Signal the stream to stop and block until it completes."""
340+ def _stop_no_join (self ):
341+ """Signal the request stream to stop.
342+
343+ To actually stop the worker ("consumer thread"), a ``STOP`` is
344+ sent to the request queue.
345+
346+ The ``_consumer_thread`` member is removed from the current instance
347+ and returned.
348+
349+ Returns:
350+ threading.Thread: The worker ("consumer thread") that is being
351+ stopped.
352+ """
341353 self .active = False
342354 self ._exiting .set ()
343355 _LOGGER .debug ('Stopping helper thread %s' , self ._consumer_thread .name )
344356 self .send_request (_helper_threads .STOP )
345- self ._consumer_thread . join ()
357+ thread = self ._consumer_thread
346358 self ._consumer_thread = None
359+ return thread
360+
361+ def stop_consuming (self ):
362+ """Signal the stream to stop and block until it completes.
363+
364+ To actually stop the worker ("consumer thread"), a ``STOP`` is
365+ sent to the request queue.
366+
367+ This **assumes** that the caller is not in the same thread
368+ (since a thread cannot ``join()`` itself).
369+ """
370+ thread = self ._stop_no_join ()
371+ thread .join ()
0 commit comments