Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve tempfile handling in checkpoint.py #281

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,35 @@ def copy_to_shm(file: str):
yield file
return

tmp_dir = "/dev/shm/"
fd, tmp_path = tempfile.mkstemp(dir=tmp_dir)
try:
shutil.copyfile(file, tmp_path)
yield tmp_path
finally:
os.remove(tmp_path)
os.close(fd)
with tempfile.NamedTemporaryFile(dir="/dev/shm", delete=False) as tmp_file:
tmp_path = tmp_file.name
try:
shutil.copyfile(file, tmp_path)
yield tmp_path
finally:
try:
os.remove(tmp_path)
except OSError as e:
# Handle file deletion error gracefully
logger.error(f"Error deleting temporary file: {e}")
raise


@contextlib.contextmanager
def copy_from_shm(file: str):
tmp_dir = "/dev/shm/"
fd, tmp_path = tempfile.mkstemp(dir=tmp_dir)
try:
yield tmp_path
shutil.copyfile(tmp_path, file)
finally:
os.remove(tmp_path)
os.close(fd)
with tempfile.NamedTemporaryFile(dir=tmp_dir, delete=False) as tmp_file:
tmp_path = tmp_file.name
try:
yield tmp_path
shutil.copyfile(tmp_path, file)
finally:
try:
os.remove(tmp_path)
except OSError as e:
# Handle file deletion error gracefully
logger.error(f"Error deleting temporary file: {e}")
raise


def fast_unpickle(path: str) -> Any:
Expand Down