@@ -14,7 +14,6 @@ def __init__(self, lock_name: str):
1414 self .shm = create_or_link_shm (self .lock_name , self .dest_size )
1515
1616 self .shm .buf .cast ("i" )[0 ] = 0
17- self .acquire_time = None
1817 return
1918
2019 def __enter__ (self ):
@@ -30,24 +29,20 @@ def __exit__(self, exc_type, exc_val, exc_tb):
3029
3130 # acquire_sleep1ms 和 release 是某些特定场景下主动使用进行锁获取的操作函数
3231 def acquire_sleep1ms (self ):
33- last_log_time = time .monotonic ()
32+ start_time = time .monotonic ()
33+ is_first = True
3434 with atomics .atomicview (buffer = self .shm .buf , atype = atomics .INT ) as a :
3535 while not a .cmpxchg_weak (0 , 1 ):
36- now = time .monotonic ()
37- if now - last_log_time >= 0.1 :
38- logger .warning ("acquire_sleep1ms wait for 100ms" )
39- last_log_time = now
36+ # 减少日志数量
37+ if is_first :
38+ is_first = False
39+ logger .warning ("acquire_sleep1ms wait for 1ms" )
40+ if time .monotonic () - start_time > 0.010 :
41+ logger .warning ("acquire_sleep1ms wait more than 10ms" )
4042 time .sleep (0.001 )
4143 pass
42- self .acquire_time = time .monotonic ()
43-
44- def release (self , log_timeout = False , log_tag = "" ):
45- if log_timeout and self .acquire_time is not None :
46- hold_time = (time .monotonic () - self .acquire_time ) * 1000 # 转换为毫秒
47- if hold_time > 5 :
48- tag_str = f"[{ log_tag } ] " if log_tag else ""
49- logger .warning (f"{ tag_str } Lock { self .lock_name } held for { hold_time :.2f} ms (>5ms)" )
50- self .acquire_time = None
44+
45+ def release (self ):
5146 with atomics .atomicview (buffer = self .shm .buf , atype = atomics .INT ) as a :
5247 while not a .cmpxchg_weak (1 , 0 ):
5348 pass
0 commit comments