python逐行讀取子進程的輸出代碼

cgdf 9年前發布 | 2K 次閱讀 Python

python中默認情況下是等待子進程完成之后,一下讀取子進程的輸出,要想逐行讀取,需要自己做一點小技巧。

import sys,os,socket
import datetime
import fcntl
import subprocess
from threading import Thread

def log_worker(stdout): ''' needs to be in a thread so we can read the stdout w/o blocking ''' username, hostname = os.environ.get('USER'), socket.gethostname() log_file = '/var/log/mysql-%s.log' % username log = open(log_file, 'a') while True: output = non_block_read(stdout).strip() if output: ''' [Tue Oct 30 22:13:13 2012 cseibert@host1]> ''' prompt = '[%(timestamp)s %(username)s@%(host)s]> \n' % dict( timestamp=datetime.datetime.now().strftime('%a %b %d %H:%M:%S %Y'), username=username, host=hostname) print prompt + output log.write(prompt + output + '\n') log.close()

def non_block_read(output): ''' even in a thread, a normal read with block until the buffer is full ''' 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 ''

if name == 'main': sub_process = subprocess.Popen( ['sh', '/root/test/dummy.sh'], stdin=sys.stdin, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

thread = Thread(target=log_worker, args=[sub_process.stdout])
thread.daemon = True
thread.start()

sub_process.wait()
thread.join(timeout=1)</pre> 


 本文由用戶 cgdf 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!