@@ -85,6 +85,8 @@ def __init__(self, targets="127.0.0.1",
8585 self .__io_queue = Queue ()
8686 self .__ioerr_queue = Queue ()
8787 self .__process_killed = threading .Event ()
88+ self .__thread_stdout = None
89+ self .__thread_stderr = None
8890
8991 # API usable in callback function
9092 self .__state = self .READY
@@ -97,7 +99,6 @@ def __init__(self, targets="127.0.0.1",
9799 self .__summary = ''
98100 self .__stdout = ''
99101 self .__stderr = ''
100- self .initial_threads = 0
101102
102103 def _run_init (self ):
103104 """
@@ -215,20 +216,29 @@ def stream_reader(thread_stdout, io_queue):
215216 stdout = subprocess .PIPE ,
216217 stderr = subprocess .PIPE ,
217218 bufsize = 0 )
218- self .initial_threads = threading .active_count ()
219- Thread (target = stream_reader , name = 'stdout-reader' ,
220- args = (self .__nmap_proc .stdout ,
221- self .__io_queue )).start ()
222- Thread (target = stream_reader , name = 'stderr-reader' ,
223- args = (self .__nmap_proc .stderr ,
224- self .__ioerr_queue )).start ()
219+ self .__thread_stdout = Thread (target = stream_reader ,
220+ name = 'stdout-reader' ,
221+ args = (self .__nmap_proc .stdout ,
222+ self .__io_queue ))
223+ self .__thread_stderr = Thread (target = stream_reader ,
224+ name = 'stderr-reader' ,
225+ args = (self .__nmap_proc .stderr ,
226+ self .__ioerr_queue ))
227+
228+ self .__thread_stdout .start ()
229+ self .__thread_stderr .start ()
230+
225231 self .__state = self .RUNNING
226232 except OSError :
227233 self .__state = self .FAILED
228234 raise
229235
230236 return self .__wait ()
231237
238+ def active_fd (self ):
239+ return (self .__thread_stdout .is_alive () or
240+ self .__thread_stderr .is_alive ())
241+
232242 def __wait (self ):
233243 """
234244 Private method, called by run() which will loop and
@@ -243,8 +253,9 @@ def __wait(self):
243253 """
244254 thread_stream = ''
245255 while (self .__nmap_proc .poll () is None or
246- threading .active_count () != self .initial_threads or
247- not self .__io_queue .empty ()):
256+ self .active_fd () is True or
257+ not self .__io_queue .empty () or
258+ not self .__ioerr_queue .empty ()):
248259 if self .__process_killed .isSet ():
249260 break
250261 try :
@@ -276,7 +287,6 @@ def __wait(self):
276287 return self .rc
277288
278289 def run_background (self ):
279- self .daemon = True
280290 super (NmapProcess , self ).start ()
281291
282292 def is_running (self ):
0 commit comments