From 880669b05089d65292eb11599b62dcf3ecd6259a Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Mon, 6 Nov 2023 21:54:19 +0800 Subject: [PATCH] remove flush lock with swap, why bother a lock? --- lib_not_dr/logger/outstream.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib_not_dr/logger/outstream.py b/lib_not_dr/logger/outstream.py index d9a427c..731c2ef 100644 --- a/lib_not_dr/logger/outstream.py +++ b/lib_not_dr/logger/outstream.py @@ -90,7 +90,6 @@ class FileCacheOutputStream(BaseOutputStream): # 默认 10 次 flush 一次 flush_count_limit: int = 10 flush_time_limit: int = 10 # time limit in sec, 0 means no limit - flush_lock: threading.Lock = None flush_timer: threading.Timer = None file_path: Optional[Path] = Path('./logs') @@ -120,7 +119,6 @@ class FileCacheOutputStream(BaseOutputStream): self.file_start_time = round(time.time()) if self.text_cache is None: self.text_cache = io.StringIO() - self.flush_lock = threading.Lock() self.get_file_path() return False @@ -213,9 +211,8 @@ class FileCacheOutputStream(BaseOutputStream): def flush(self) -> None: new_cache = io.StringIO() # 创建新的缓存 self.flush_counter = 0 # atomic, no lock - with self.flush_lock: - text = self.text_cache.getvalue() - old_cache, self.text_cache = self.text_cache, new_cache + old_cache, self.text_cache = self.text_cache, new_cache + text = old_cache.getvalue() old_cache.close() # 关闭旧的缓存 if text == '': return None