Skip to content

Allow repeatedly asking for termination of QueueListener  #114706

Closed
@Booplicate

Description

@Booplicate

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

self.enqueue_sentinel()
self._thread.join()
self._thread = None

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

Labels

stdlibPython modules in the Lib dirtype-featureA feature request or enhancement

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions