资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

pythonsubprocess模块监控子进程的2种方式忙等待和立即返回同时设置子进程超时

下面的资料是关于python subprocess模块 监控子进程的2种方式 忙等待和立即返回同时设置子进程超时时间的代码。

成都创新互联公司2013年开创至今,先为文安等服务建站,文安等地企业,进行企业商务咨询服务。为文安企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

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)  

文章题目:pythonsubprocess模块监控子进程的2种方式忙等待和立即返回同时设置子进程超时
文章网址:http://cdkjz.cn/article/jdspcd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220