您好,登錄后才能下訂單哦!
下面的資料是關于python subprocess模塊 監控子進程的2種方式 忙等待和立即返回同時設置子進程超時時間的代碼。
import subprocess
import os
import time
tt = '555'
cmd = "python /home/100003/python/mypython/sub2.py "+" 333"+" 444 "+tt
print 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'
break
print time.time()
二:子進程結束立即返回使用select模塊同時可設置子進程的超時時間
import subprocess
import select
import time
import signal
import os
tt = '555'
cmd = "python /home/100003/python/mypython/sub2.py "+" 333"+" 444 "+tt
timeout = 3
pro = 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)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。