From 788b248dbcebfbdf7b3bfd00a48d92930ab7f18b Mon Sep 17 00:00:00 2001 From: cui0711 <1729461967@qq.com> Date: Fri, 30 Jan 2026 16:27:49 +0800 Subject: [PATCH] fix(logger): add Windows platform support for file locking --- lib_results_logger.py | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/lib_results_logger.py b/lib_results_logger.py index a33df50..9c81b89 100644 --- a/lib_results_logger.py +++ b/lib_results_logger.py @@ -7,10 +7,19 @@ Appends task completion results to results.json in real-time. import json import os import time -import fcntl +import platform from pathlib import Path from typing import Dict, Any, Optional +# Import fcntl only on Unix-like systems (Linux, macOS) +# On Windows, we'll use msvcrt for file locking +if platform.system() != "Windows": + import fcntl + HAS_FCNTL = True +else: + import msvcrt + HAS_FCNTL = False + def extract_domain_from_path(result_path: str) -> str: """ @@ -66,8 +75,12 @@ def append_task_result( # Thread-safe JSON append with file locking try: with open(results_file, 'a+') as f: - # Lock the file for exclusive access - fcntl.flock(f.fileno(), fcntl.LOCK_EX) + # Lock the file for exclusive access (platform-specific) + if HAS_FCNTL: + fcntl.flock(f.fileno(), fcntl.LOCK_EX) + else: + # Windows file locking using msvcrt + msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, 1) try: # Move to beginning to read existing content @@ -95,8 +108,12 @@ def append_task_result( f.write('\n') # Add newline for readability finally: - # Always unlock the file - fcntl.flock(f.fileno(), fcntl.LOCK_UN) + # Always unlock the file (platform-specific) + if HAS_FCNTL: + fcntl.flock(f.fileno(), fcntl.LOCK_UN) + else: + # Windows unlock using msvcrt + msvcrt.locking(f.fileno(), msvcrt.LK_UNLCK, 1) print(f"📝 Logged result: {domain}/{task_id} -> {result_entry['status']} (score: {score})")