@@ -23,6 +23,8 @@ def __init__(self):
2323 self .timeout = None
2424 self .work_queue = Queue ()
2525 self .in_scan = False
26+ self .callback_count = 0
27+ self .callback_count_lock = Lock ()
2628 self .pause_lock = Lock () # 暂停锁
2729 self .worker_count : int = 0 # 线程数
2830 self .working_worker : int = 0 # 工作中的线程数
@@ -78,7 +80,14 @@ def scan_a_port(self, port: int, callback: Any):
7880 if raw_info ["status" ] == "online" :
7981 Thread (target = callback , args = (ServerInfo (raw_info ["info" ]),)).start ()
8082 return
81- Thread (target = callback , args = (raw_info ,)).start ()
83+ Thread (target = self ._callback , args = (raw_info , callback )).start ()
84+
85+ def _callback (self , raw_info , callback : Any ):
86+ with self .callback_count_lock :
87+ self .callback_count += 1
88+ callback (raw_info )
89+ with self .callback_count_lock :
90+ self .callback_count -= 1
8291
8392 def scan_worker (self ):
8493 with self .thread_num_lock :
@@ -108,7 +117,12 @@ def scan_worker(self):
108117 self .worker_count -= 1
109118 self .working_worker -= 1
110119 if self .worker_count <= 0 :
111- self .in_scan = False
120+ Thread (target = self .check_callback_over_thread , daemon = True ).start ()
121+
122+ def check_callback_over_thread (self ):
123+ while self .callback_count > 0 :
124+ sleep (0.05 )
125+ self .in_scan = False
112126
113127
114128class Port :
0 commit comments