[New-bugs-announce] [issue41483] Do not acquire lock in MemoryHandler.flush() if no target defined

Kostis Anagnostopoulos report at bugs.python.org
Wed Aug 5 02:06:13 EDT 2020


New submission from Kostis Anagnostopoulos <ankostis at gmail.com>:

The `logging.handlers.MemoryHandler.flush()` method acquire the lock even if target has not been set, and the method is a noop:

```
def flush(self):
    # (Docstring skipped)
    self.acquire()
    try:
        if self.target:
	    for record in self.buffer:
	        self.target.handle(record)
	    self.buffer..clear()
    finally:
        self.release()
```

An optimized version would re-arrrange the nesting to avoid the locking:

```
def flush(self):
    # (Docstring skipped)
    if self.target:
        self.acquire()
        try:
            for record in self.buffer:
                self.target.handle(record)
            self.buffer.clear()
        finally:
            self.release()
```

- There is no use-case, beyond the expected performance gain.
- Without having searched, i would deem highly improbable the existence of code in the std-library or 3rdp code that depends on the current noop-locking when `flush()` is called.

----------
components: Library (Lib)
messages: 374859
nosy: ankostis, penlect, vinay.sajip
priority: normal
severity: normal
status: open
title: Do not acquire lock in MemoryHandler.flush() if no target defined
type: performance
versions: Python 3.10, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41483>
_______________________________________


More information about the New-bugs-announce mailing list