python subprocess模块 监控子进程的2种方式 忙等待和立即返回同时设置子进程超时时间 - 转
2013-05-03 16:43
609 查看
/article/4221512.html
一:循环 忙等 子进程结束
二:子进程结束 立即返回 使用select模块 同时可设置子进程的超时时间
一:循环 忙等 子进程结束
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)
相关文章推荐
- python subprocess模块 监控子进程的2种方式 忙等待和立即返回同时设置子进程超时时间
- 监控函数的执行时间,若超时则强制返回一个默认值
- 设置socket超时时间的两种方式
- 用systemd管理进程时,修改复位超时等待时间
- android HttpUrlConnection 设置超时无效,等待时间很长的问题
- 关于http socket timeout 超时时间 未设置 导致线程一直在等待(线程饥饿),微信公众号开发过程遇到的。java
- 网站访问状态和超时时间监控报警设置
- C# 对一个函数设置执行的超时时间、函数执行的等待时间、等待结果的时间、允许的执行时间
- 设置MySQL 超时等待时间
- selenium设置元素发现超时等待时间-python
- php同时作为server端和client端(soapclient)的超时时间设置小结
- 怎么设置session无响应超时时间并且自动返回登陆页面
- CXF 客户端超时时间设置(非Spring配置方式)
- java 方法的执行时间监控 设置超时(Future 接口)
- 为何要设置数据库连接池的 超时等待时间
- 关于http socket timeout 超时时间 未设置 导致线程一直在等待(线程饥饿),微信公众号开发过程遇到的。java
- 检查一个路径下文件是否存在,如果不存在设置一个定时器,在定时器内每隔一定时间检查一次,直到该文件存在返回成功,或者定时超时返回失败
- HttpClientUtils.post不设置超时时间容易导致,请求无法返回
- Windows系统设置开机菜单选择超时等待时间的方法
- WebDriverWait等设置等待时间和超时时间