|
1 | 1 | # Copyright (c) Alibaba, Inc. and its affiliates. |
2 | 2 | import datetime as dt |
| 3 | +import fcntl |
| 4 | +import hashlib |
3 | 5 | import os |
4 | 6 | import random |
5 | 7 | import re |
|
11 | 13 |
|
12 | 14 | import numpy as np |
13 | 15 | import torch.distributed as dist |
| 16 | +from modelscope.hub.utils.utils import get_cache_dir |
14 | 17 | from transformers import HfArgumentParser, enable_full_determinism, set_seed |
15 | 18 |
|
16 | 19 | from .logger import get_logger |
|
20 | 23 | logger = get_logger() |
21 | 24 |
|
22 | 25 |
|
| 26 | +class FileLockContext: |
| 27 | + |
| 28 | + cache_dir = os.path.join(get_cache_dir(), 'lockers') |
| 29 | + |
| 30 | + def __init__(self, origin_symbol: str, timeout: int = 60 * 30): |
| 31 | + self.origin_symbol = origin_symbol |
| 32 | + self.file_path = hashlib.md5(origin_symbol.encode('utf-8')).hexdigest() + '.lock' |
| 33 | + self.file_path = os.path.join(FileLockContext.cache_dir, self.file_path) |
| 34 | + self.file_handle = None |
| 35 | + self.timeout = timeout |
| 36 | + |
| 37 | + def acquire(self): |
| 38 | + """Acquire the lock, optionally waiting until it is available.""" |
| 39 | + start_time = time.time() |
| 40 | + while True: |
| 41 | + try: |
| 42 | + os.makedirs(FileLockContext.cache_dir, exist_ok=True) |
| 43 | + open(self.file_path, 'a').close() |
| 44 | + self.file_handle = open(self.file_path, 'w') |
| 45 | + fcntl.flock(self.file_handle, fcntl.LOCK_EX) |
| 46 | + return True |
| 47 | + except IOError as e: |
| 48 | + if self.file_handle: |
| 49 | + self.file_handle.close() |
| 50 | + self.file_handle = None |
| 51 | + if self.timeout and (time.time() - start_time) >= self.timeout: |
| 52 | + raise IOError(f'Cannot acquire the file lock from {self.origin_symbol} ' |
| 53 | + f'as the timeout reaches: {self.timeout} seconds') from e |
| 54 | + time.sleep(1) |
| 55 | + |
| 56 | + def release(self): |
| 57 | + """Release the lock.""" |
| 58 | + if self.file_handle: |
| 59 | + fcntl.flock(self.file_handle, fcntl.LOCK_UN) |
| 60 | + self.file_handle.close() |
| 61 | + self.file_handle = None |
| 62 | + |
| 63 | + def __enter__(self): |
| 64 | + self.acquire() |
| 65 | + return self |
| 66 | + |
| 67 | + def __exit__(self, exc_type, exc_value, traceback): |
| 68 | + self.release() |
| 69 | + |
| 70 | + |
23 | 71 | @contextmanager |
24 | 72 | def safe_ddp_context(): |
25 | 73 | if is_dist() and not is_local_master(): |
|
0 commit comments