|
|
一:循环 忙等 子进程结束
import subprocessimport osimport timett = '555'cmd = "python /home/100003/python/mypython/sub2.py "+" 333"+" 444 "+ttprint time.time()sub2 = subprocess.Popen(cmd, shell=True)while 1: ret1 = subprocess.Popen.poll(sub2) if ret1 == 0: print sub2.pid,'end' break elif ret1 is None: print 'running' time.sleep(1) else: print sub2.pid,'term' breakprint time.time()
二:子进程结束 立即返回 使用select模块 同时可设置子进程的超时时间
import subprocessimport selectimport timeimport signalimport ostt = '555'cmd = "python /home/100003/python/mypython/sub2.py "+" 333"+" 444 "+tttimeout = 3pro = subprocess.Popen(cmd, stdout=subprocess.PIPE,shell = True)print time.time()while 1: while_begin = time.time() print 'timeout',timeout fs = select.select([pro.stdout], [], [], timeout) if pro.stdout in fs[0]: tmp = pro.stdout.read() print 'read', tmp if not tmp: print 'end' print time.time() break else: print 'outoftime' print os.kill(pro.pid, signal.SIGKILL), break timeout = timeout - (time.time() - while_begin) |
|