Import selectimport osimport subprocessimport timeimport fcntlargs = [' Python ', './fetch_file2.py ', Ip,path]proc = Subprocess. Popen (args, stdout=subprocess. Pipe,stderr=subprocess. PIPE,CLOSE_FDS=TRUE) def non_block_read (output): # Avoid blocking FD = Output.fileno () FL = Fcntl.fcntl (FD, Fcntl. F_GETFL) Fcntl.fcntl (FD, Fcntl. F_SETFL, FL | Os. O_nonblock) Try:return output.read () Except:return "" While Proc.poll () is None: #fetch中rsync结束. But fetch does not end (suspect output is too large) cause here has been none pass Print proc.poll () # Kill Fetch Process return -9print Proc.stderr.read () #阻塞
#方法1: #non_block_read (proc.stderr) #防止阻塞
#方法2: Select_rfds = [Proc.stdout, Proc.stderr] (RfDs, Wfds, Efds) = Select.select (Select_rfds, [],[]) if proc.stderr in RfDs : #不存在. If Select_rfds=[stderr], then block on select len = Proc.stderr.read (Ten) If len = = 0:print "Empty" Else:print "Proc.s Tderr "If proc.stdout in Rfds:print" Proc.stdout "
Python subprocess Blocking