下面的资料是关于python subprocess模块 监控子进程的2种方式 忙等待和立即返回同时设置子进程超时时间的代码。import subprocess import os import time tt = 555 cmd = python /home/100003/python/my...
下面的资料是关于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)
沃梦达教程
本文标题为:python subprocess模块 监控子进程的2种方式 忙等待和立即返回同时设置子进程超时
基础教程推荐
猜你喜欢
- Centos7下安装python环境 2023-09-04
- Python基础学习之函数和代码复用详解 2022-09-02
- python的环境conda简介 2022-10-20
- Python 中 Elias Delta 编码详情 2023-08-08
- ubuntu 18 python3.6 的安装与 python2的版本切换 2023-09-03
- centos系统 anaconda3(python3)安装pygrib 2023-09-04
- Python爬取当网书籍数据并数据可视化展示 2023-08-11
- CentOS 7.5 安装 Python3.7 2023-09-03
- 四步教你学会打包一个新的Python模块 2022-10-20
- 基于Python实现股票数据分析的可视化 2023-08-04