FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-150818: Speed up logging.getLogger() for existing loggers (GH-150825) by gaborbernat · Pull Request #150825 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (1) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
9 changes: 8 additions & 1 deletion Lib/logging/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1373,9 +1373,16 @@ def getLogger(self, name):
logger and fix up the parent/child references which pointed to the
placeholder to now point to the logger.
"""
rv = None
if not isinstance(name, str):
raise TypeError('A logger name must be a string')
# Fast path: an already-registered, non-placeholder logger can be
# returned without taking the lock. dict.get() is atomic under both
# the GIL and free threading, and a Logger is fully initialised before
# being inserted into loggerDict under the lock, so this never sees a
# partially-constructed object.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

self._fixupParents(rv) is called after self.loggerDict[name] = rv.
Therefore, it is possible to get a logger whose logger.parent has not yet been set.

rv = self.loggerDict.get(name)
if rv is not None and not isinstance(rv, PlaceHolder):
return rv
with _lock:
if name in self.loggerDict:
rv = self.loggerDict[name]
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Speed up :func:`logging.getLogger` with a lock-free fast path that returns an
already-registered logger without acquiring the logging lock. Patch by Bernát
Gábor.
Loading

Back | FazBrowse Home | New Git URL