在论坛上找到方法了,http://bbs.csdn.net/topics/340234292
http://blog.csdn.net/junshao90/article/details/8215751
- import subprocess
- def main():
- process1 = subprocess.Popen("python -u sub.py", shell=False, stdout = subprocess.PIPE, stderr=subprocess.STDOUT)
- #print process1.communicate()[0]
- while True:
- line = process1.stdout.readline()
- if not line:
- break
- print line
- if __name__ == '__main__':
- main()
- import subprocess
- import time
- p = subprocess.Popen('ping 127.0.0.1 -n 10', stdout=subprocess.PIPE)
- while p.poll() == None:
- print p.stdout.readline()
- time.sleep(1)
- print p.stdout.read()
- print 'returen code:', p.returncode