Skip to content

Commit

Permalink
Merge pull request #178 from MAIF/feature/fixed_del
Browse files Browse the repository at this point in the history
⚡ Remove monkeypatch on pandas del method
  • Loading branch information
HugoPerrier authored Sep 5, 2024
2 parents 82e0381 + 020489a commit a265c56
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 38 deletions.
37 changes: 0 additions & 37 deletions melusine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
"""

import logging
from ctypes import CDLL, cdll
from typing import Any, Optional

import pandas as pd

from melusine._config import config

Expand All @@ -19,36 +15,3 @@
# LOGGING
# ------------------------------- #
logging.getLogger(__name__).addHandler(logging.NullHandler())


# ------------------------------- #
# MONKEY PATCH
# ------------------------------- #

# Monkey patch for pandas DataFrame memory leaking on linux OS (pandas issue #2659)
try:
# Try executing linux malloc_trim function (release free memory)
cdll.LoadLibrary("libc.so.6")
libc: Optional[CDLL] = CDLL("libc.so.6")
if libc is not None:
libc.malloc_trim(0)
except (OSError, AttributeError): # pragma: no cover
# Incompatible OS: this monkey patch is not needed
libc = None

# Store the standard pandas method
__std_del: Optional[Any] = getattr(pd.DataFrame, "__del__", None)


# Prepare a new __del__ method
def __fixed_del(self: Any) -> None: # pragma: no cover
"""Override DataFrame's __del__ method: call the standard method + release free memory with malloc_trim."""
if __std_del is not None:
__std_del(self)
if libc is not None:
libc.malloc_trim(0)


# Override standard pandas method if needed
if libc is not None:
pd.DataFrame.__del__ = __fixed_del
2 changes: 1 addition & 1 deletion melusine/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def transform(self, df: MelusineDataset) -> MelusineDataset:
return_cols.append(self.debug_dict_col)

for method in self.transform_methods:
logger.debug(f"Running transform for {type(self).__name__} ({method.__name__})")
logger.debug("Running transform for %s (%s)", type(self).__name__, getattr(method, "__name__", "unnamed"))
first_arg_name: str = list(inspect.signature(method).parameters)[0]

if first_arg_name == "row":
Expand Down

0 comments on commit a265c56

Please sign in to comment.