Chinaunix首页 | 论坛 | 博客
  • 博客访问: 276352
  • 博文数量: 103
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 705
  • 用 户 组: 普通用户
  • 注册时间: 2013-05-02 16:15
文章分类

全部博文(103)

文章存档

2014年(8)

2013年(95)

我的朋友

分类: Python/Ruby

2013-12-18 21:53:56

subprocess
特点:
    调用外部系统命令创建新的子进程
    能够使用input/output/error
    有返回值
     可以替代旧的os.system,os.popen,commands
import subprocess
ping = subprocess.Popen(args="ping -c 4 )
print ping.pid
print ping.returncode


import subprocess
ping = subprocess.Popen(args="ping -c 4 )
print ping.pid
print ping.returncode
---------------------------------------------------------------------------------------------
使用wait()对进程进行控制,等待子进程执行完毕,在执行下面的语句
import subprocess
ping = subprocess.Popen(args="ping -c 4 )
ping.wait()
print ping.pid
print ping.returncode


PING (115.239.210.26) 56(84) bytes of data.
64 bytes from 115.239.210.26: icmp_req=1 ttl=127 time=32.0 ms
64 bytes from 115.239.210.26: icmp_req=2 ttl=127 time=33.9 ms
64 bytes from 115.239.210.26: icmp_req=3 ttl=127 time=31.7 ms
64 bytes from 115.239.210.26: icmp_req=4 ttl=127 time=31.0 ms

--- ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 25266ms
rtt min/avg/max/mdev = 31.013/32.192/33.903/1.061 ms
2754
0

--------------------------------------------------------------------------------------
stdout/stdin/stderr

import subprocess
ping = subprocess.Popen(args="ping -c 4 )
ping.wait()
print ping.stdout.read()
print ping.pid
print ping.returncode
---------------------------------------------------------------------------

阅读(1222) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~