bash_path = r'F:\Program Files\Git\bin\bash.exe' defthread_for_listen(subp,q): for line initer(subp.stdout.readline,''): q.put(line) subp = subprocess.Popen(bash_path,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,encoding='utf8',cwd='./public') q = queue.Queue() p1 = threading.Thread(target =thread_for_listen,args=(subp,q))
p1.start() for i inrange(5): subp.stdin.write('nvm -v\n') subp.stdin.flush() print(q.get(),end='') time.sleep(0.3) print('wake up after 0.3s') subp.stdin.close() q.task_done()
output
1 2 3 4 5 6 7 8 9 10
1.1.11 wake up after 0.3s 1.1.11 wake up after 0.3s 1.1.11 wake up after 0.3s 1.1.11 wake up after 0.3s 1.1.11 wake up after 0.3s