-
Notifications
You must be signed in to change notification settings - Fork 54
fix: add cross-platform file locking and background progress thread #414
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
Open
jschen069
wants to merge
6
commits into
AISBench:master
Choose a base branch
from
jschen069:fix_lock_json_0723
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2bc002c
fix: add cross-platform file locking and background progress thread
jschen069 c41b9d6
修复用例
jschen069 48feb84
refactor: remove win32 cross-platform file locking, keep fcntl only f…
jschen069 1258b15
revert: remove background progress thread changes from commit 2bc002c
jschen069 3bbb813
删除无用导入
jschen069 cf3cd25
打印日志
jschen069 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,42 +20,72 @@ | |
|
|
||
| logger = AISLogger() | ||
|
|
||
|
|
||
| import fcntl | ||
|
|
||
|
|
||
| def _lock_file(f): | ||
| """Blocking exclusive lock on an open file.""" | ||
| fcntl.lockf(f.fileno(), fcntl.LOCK_EX) | ||
|
|
||
|
|
||
| def _unlock_file(f): | ||
| """Release lock on an open file.""" | ||
| try: | ||
| fcntl.lockf(f.fileno(), fcntl.LOCK_UN) | ||
| except IOError as e: | ||
| logger.warning(f"Failed to release file lock: {e}") | ||
|
|
||
|
|
||
| def write_status(file_path, status): | ||
| """Write status to a JSON file, appending to existing content. | ||
|
|
||
| Uses file locking to safely handle concurrent writes from multiple | ||
| processes to the same file (read-modify-write is atomic). | ||
|
|
||
| Args: | ||
| file_path: Path to the status file | ||
| status: Status data to append | ||
|
|
||
| Returns: | ||
| bool: True if successful, False otherwise | ||
| """ | ||
| # read existing content | ||
| existing_data = [] | ||
| if os.path.exists(file_path): | ||
| # Ensure the file exists before opening in r+ mode | ||
| if not os.path.exists(file_path): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这儿是会被多线程调用?如果两个线程同时调用了这个函数,后面一个线程已经写了一些内容,当第二个线程走到这会导致文件内容被清除?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| try: | ||
| with open(file_path, 'r', encoding='utf-8') as f: | ||
| existing_data = json.load(f) | ||
| except json.JSONDecodeError as e: | ||
| logger.warning( | ||
| f"Failed to parse JSON from status file '{file_path}': {e}. " | ||
| f"Starting with empty status list." | ||
| ) | ||
| existing_data = [] | ||
| except IOError as e: | ||
| logger.warning( | ||
| f"Failed to read status file '{file_path}': {e}. " | ||
| f"Starting with empty status list." | ||
| ) | ||
| existing_data = [] | ||
|
|
||
| # add new status | ||
| existing_data.append(status) | ||
| with open(file_path, 'w', encoding='utf-8') as f: | ||
| json.dump([], f) | ||
| except IOError: | ||
| pass | ||
|
|
||
| # write to file | ||
| try: | ||
| with open(file_path, 'w', encoding='utf-8') as f: | ||
| json.dump(existing_data, f) | ||
| with open(file_path, 'r+', encoding='utf-8') as f: | ||
| _lock_file(f) | ||
| try: | ||
| # read existing content | ||
| content = f.read() | ||
| if content.strip(): | ||
| try: | ||
| existing_data = json.loads(content) | ||
| except json.JSONDecodeError as e: | ||
| logger.warning( | ||
| f"Failed to parse JSON from status file '{file_path}': {e}. " | ||
| f"Starting with empty status list." | ||
| ) | ||
| existing_data = [] | ||
| else: | ||
| existing_data = [] | ||
|
|
||
| # add new status | ||
| existing_data.append(status) | ||
|
|
||
| # write back | ||
| f.seek(0) | ||
| f.truncate() | ||
| json.dump(existing_data, f) | ||
| f.flush() | ||
| finally: | ||
| _unlock_file(f) | ||
| return True | ||
| except IOError as e: | ||
| logger.warning(f"Failed to write status to '{file_path}': {e}") | ||
|
|
@@ -82,17 +112,29 @@ def read_and_clear_statuses(tmp_file_dir, tmp_file_name_list): | |
| logger.debug(f"Reading and clearing {len(abs_path_list)} status files from '{tmp_file_dir}'") | ||
|
|
||
| for tmp_file in abs_path_list: | ||
| try: | ||
| # read existing content | ||
| with open(tmp_file, 'r', encoding='utf-8') as f: | ||
| data = json.load(f) | ||
|
|
||
| status_count = len(data) | ||
| all_status.extend(data) | ||
| if not os.path.exists(tmp_file): | ||
| continue | ||
|
|
||
| # clear file content | ||
| with open(tmp_file, 'w', encoding='utf-8') as f: | ||
| json.dump([], f) | ||
| try: | ||
| with open(tmp_file, 'r+', encoding='utf-8') as f: | ||
| _lock_file(f) | ||
| try: | ||
| content = f.read() | ||
| if content.strip(): | ||
| data = json.loads(content) | ||
| else: | ||
| data = [] | ||
|
|
||
| status_count = len(data) | ||
| all_status.extend(data) | ||
|
|
||
| # clear file content | ||
| f.seek(0) | ||
| f.truncate() | ||
| json.dump([], f) | ||
| f.flush() | ||
| finally: | ||
| _unlock_file(f) | ||
|
|
||
| logger.debug(f"Read {status_count} statuses from '{tmp_file}' and cleared file") | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fcntl 属于 unix 下的 module,如果在 windows 上跑会不会报错?还是说我们不会在 WINDOWS 上跑?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aisbench不会在windows跑