Skip to content

Commit

Permalink
Fix memory mapping on Windows (#193)
Browse files Browse the repository at this point in the history
* Fix memory mapping on Windows

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
belerico and pre-commit-ci[bot] authored Jan 19, 2024
1 parent 2c9c0b3 commit 5163fd1
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions sheeprl/utils/memmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import numpy as np
from numpy.typing import DTypeLike

from sheeprl.utils.imports import _IS_WINDOWS


def is_shared(array: np.ndarray) -> bool:
return isinstance(array, np.ndarray) and hasattr(array, "_mmap")
Expand Down Expand Up @@ -214,12 +212,12 @@ def from_array(

def __del__(self) -> None:
"""Delete the memory-mapped array. If the memory-mapped array has ownership of the file and no other
reference to the memory-mapped array exists or the OS is Windows-based,
reference to the memory-mapped array exists,
then the memory-mapped array will be flushed to disk and both the memory-mapped array and
the file will be closed. If the memory-mapped array is mapped to a temporary file then the file is
removed.
"""
if (self._has_ownership and getrefcount(self._file) <= 2) or _IS_WINDOWS:
if self._array is not None and self._has_ownership and getrefcount(self._file) <= 2:
self._array.flush()
self._array._mmap.close()
del self._array._mmap
Expand Down

0 comments on commit 5163fd1

Please sign in to comment.