Replace fcntl with filelock for Windows compatibility

This commit is contained in:
Yasser Makram 2025-11-04 07:22:34 +00:00
parent a83646e098
commit 1e89af9862

View File

@ -5,10 +5,10 @@ Common utilities for nanochat.
import os
import re
import logging
import fcntl
import urllib.request
import torch
import torch.distributed as dist
from filelock import FileLock
class ColoredFormatter(logging.Formatter):
"""Custom formatter that adds colors to log messages."""
@ -70,11 +70,7 @@ def download_file_with_lock(url, filename, postprocess_fn=None):
if os.path.exists(file_path):
return file_path
with open(lock_path, 'w') as lock_file:
# Only a single rank can acquire this lock
# All other ranks block until it is released
fcntl.flock(lock_file.fileno(), fcntl.LOCK_EX)
with FileLock(lock_path):
# Recheck after acquiring lock (another process may have downloaded it)
if os.path.exists(file_path):
@ -94,12 +90,6 @@ def download_file_with_lock(url, filename, postprocess_fn=None):
if postprocess_fn is not None:
postprocess_fn(file_path)
# Clean up the lock file after the lock is released
try:
os.remove(lock_path)
except OSError:
pass # Ignore if already removed by another process
return file_path
def print0(s="",**kwargs):