Closed
Description
Feature or enhancement
Proposal:
Currently it's impossible to call QueueListener.stop
twice, you get a crash:
from queue import Queue
from logging import StreamHandler
from logging.handlers import (
QueueHandler,
QueueListener
)
sh = StreamHandler()
q = Queue(-1)
listener = QueueListener(
q,
sh,
respect_handler_level=True
)
listener.start()
listener.stop()
listener.stop() # Crash here: 'NoneType' object has no attribute 'join'
But it may be desirable in some cases where you can't control at which point the termination will happen, you just want it to happen. For example, having it in multiple places: on app shutdown (normal behaviour) and atexit callbacks (backup in case shutdown wasn't planned and its callbacks didn't run).
This appears to be a simple "fix" (not that it's a bug) to
cpython/Lib/logging/handlers.py
Lines 1589 to 1591 in 97fb248
since you're already setting the attribute to
None
, a simple check should do:
- self.enqueue_sentinel()
- self._thread.join()
- self._thread = None
+ if self._thread:
+ self.enqueue_sentinel()
+ self._thread.join()
+ self._thread = None
Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
No response
Linked PRs
Metadata
Metadata
Assignees
Projects
Status
Done