一段代码,备忘
import subprocess
import osimport threading
def on_exit():
print("process is dead")
def monitor(process1,onexit):
print("process id %s waiting"% (process1.pid))
process1.wait()
onexit()
command=['mplayer']+["/home/aphero/1.mp4"]
process = subprocess.Popen(command,stdout=subprocess.PIPE, preexec_fn=os.setsid)
process_monitor = threading.Thread(target=monitor, args=(process, on_exit))
process_monitor.start()
print("test poll")
ret=process.poll()
while ret is None:
returncode=process.stdout.read(512)#实时读取返回数据
print(returncode)
ret=process.poll()
print("end")