首页 > 代码库 > Python学习之进程和并发
Python学习之进程和并发
从Python2.4 以后,subprocess模块负责衍生出新的进程,和标准输入,标准输出,标准错误输出交互,并监听返回值。Subprocess模块是用来取代一些老的模块,例如os.system, os.spawn, os.popen和popen2
In [34]: subprocess.call(‘df -h‘,shell=True) Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 6.7G 1.7G 4.6G 28% / tmpfs 1.9G 0 1.9G 0% /dev/shm /dev/xvda1 485M 55M 405M 12% /boot /dev/xvdb 99G 21G 74G 22% /data Out[34]: 0
还可以引入shell变量
In [105]: subprocess.call(‘du -sh $HOME‘,shell=True) 34M /home/caribbean Out[105]: 0
使用subprocess.call时可以不显示输出。
In [170]: import subprocess In [171]: ret=subprocess.call("ping -c 5 10.10.41.10",shell=True,stdout=open(‘/dev/null‘,‘w‘),stderr=subprocess.STDOUT)
如果不关心shell命令的输出,而只是想执行shell命令,可以使用subprocess.call,如果想捕获shell命令的输出结果,可以使用subprocess.Popen. subprocess.call还会中断等待响应,subprocess.Popen不会
使用Subprocess的返回值
0 成功
1 通用错误
2 误使用shell内置变量
126 调用的命令无法执行
127 命令无法找到
128 无效的参数退出
130 脚本被Ctrl-C 终端
In [250]: subprocess.call("ls /foo",shell=True) ls: cannot access /foo: No such file or directory Out[250]: 2
In [252]: ret=subprocess.call("ls /foo",shell=True) ls: cannot access /foo: No such file or directory In [253]: if ret==0: .....: print "success" .....: else: .....: print "failure" .....: .....: failure
subprocess.call调用命令的返回值可以用在条件语句中
In [382]: if platform.system() == "Linux": .....: ret=subprocess.call(‘cp /tmp/a.txt /tmp/b.txt‘,shell=True) .....: if ret==0: .....: print "Success,the copy was made on %s %s" %(platform.system(),platform.release()) .....: .....: Success,the copy was made on Linux 2.6.32-431.el6.x86_64
使用subprocess.Popen捕获输出结果
In [399]: In [400]: import subprocess In [401]: p=subprocess.Popen(‘df -h‘,shell=True,stdout=subprocess.PIPE) In [402]: out=p.stdout.readlines() In [403]: for line in out: .....: print line.strip() .....: .....: Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 6.7G 1.7G 4.6G 28% / tmpfs 1.9G 0 1.9G 0% /dev/shm /dev/xvda1 485M 55M 405M 12% /boot /dev/xvdb 99G 21G 73G 23% /data In [404]:
Readlines()返回的是一个带换行符的列表
In [413]: print out --------> print(out) [‘Filesystem Size Used Avail Use% Mounted on\n‘, ‘/dev/mapper/VolGroup-lv_root 6.7G 1.7G 4.6G 28% /\n‘, ‘tmpfs 1.9G 0 1.9G 0% /dev/shm\n‘, ‘/dev/xvda1 485M 55M 405M 12% /boot\n‘, ‘/dev/xvdb 99G 21G 73G 23% /data\n‘] In [414]:
In [440]: for line in out: .....: print line .....: .....: Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 6.7G 1.7G 4.6G 28% / tmpfs 1.9G 0 1.9G 0% /dev/shm /dev/xvda1 485M 55M 405M 12% /boot /dev/xvdb 99G 21G 73G 23% /data In [441]: for line in out: .....: print line.strip() .....: .....: Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 6.7G 1.7G 4.6G 28% / tmpfs 1.9G 0 1.9G 0% /dev/shm /dev/xvda1 485M 55M 405M 12% /boot /dev/xvdb 99G 21G 73G 23% /data In [442]:
subprocess管道工厂
In [513]: def multi(*args): .....: for cmd in args: .....: p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE) .....: out=p.stdout.read() .....: print out .....: .....: In [514]: multi("df -h","ls -lh /tmp","tail -f /var/log/system.log") tail: cannot open `/var/log/system.log‘ for reading: No such file or directory Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 6.7G 1.7G 4.6G 28% / tmpfs 1.9G 0 1.9G 0% /dev/shm /dev/xvda1 485M 55M 405M 12% /boot /dev/xvdb 99G 22G 73G 23% /data total 6.5M -rw-r--r--. 1 root root 18K May 28 17:55 a.txt -rw-r--r--. 1 caribbean operation 52K Jul 23 12:00 baidu.html -rw-r--r--. 1 caribbean operation 18K Aug 1 16:11 b.txt -rw-r--r--. 1 daemon daemon 6.4M Aug 1 14:36 php_errors.log drwxr-xr-x. 11 root root 4.0K Jul 30 14:40 pip-build-root -rw-r--r--. 1 root root 0 Jul 31 14:39 send_msg_s1.txt -rw-r--r--. 1 root root 0 Jul 31 14:39 send_msg_s2.txt -rw-r--r--. 1 root root 0 Jul 31 14:39 send_msg_s3.txt -rw-------. 1 root root 0 Dec 17 2013 yum.log -rw-------. 1 root root 877 Jul 25 10:10 yum_save_tx-2014-07-25-10-10iJ0cju.yumtx -rw-rw-rw-. 1 mysql mysql 2.4K Jul 11 13:54 yunbaomysql2.txt -rw-rw-rw-. 1 mysql mysql 2.4K Jul 11 13:45 yunbaomysql.txt
本文出自 “Linux SA John” 博客,请务必保留此出处http://john88wang.blog.51cto.com/2165294/1533651
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。