python subprocess Popen非阻塞,读取adb日志
简单版
from threading import Thread from queue import Queue, Empty import shlex if __name__ == '__main__': print_hi('PyCharm') # Car().run() def enqueue_output(stdout, queue): with open("www.log", 'wb+') as wwwf: for line in iter(stdout.readline,b''): # print("line",line) wwwf.write(line) queue.put(line) subprocess.Popen("adb logcat -c", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, close_fds=True) process = subprocess.Popen("adb logcat", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, close_fds=True) q = Queue() t = Thread(target=enqueue_output, args=(process.stdout, q)) t.daemon = True # thread dies with the program t.start() try: # line = q.get_nowait() # or q.get(timeout=.1) line = q.get(timeout=3) # or q.get(timeout=.1) except Empty: print('no output yet') else: # got line input("请输入:")
封装版