工作,学习,生活,这里将会有一些记录. 备用域名:http://meisw.wdlinux.cn 注册 | 登陆
浏览模式: 标准 | 列表分类:pytyon

subprocess再解析

之前已经写过一篇关于Python subprocess的帖子了,subprocess是Python下标准的用于进程创建、通讯的模块,这里再补充一些,注意:我还一直坚守Python2.x,所以不一定适合Python 3。

subprocess简单用法

这是最简单的用法:

p=subprocess.Popen("dir", shell=True)
p.wait()

shell参数根据你要执行的命令的情况来决定,上面是dir命令,就一定要shell=True了,p.wait()可以得到命令的返回值,没有问题。

进程通讯

如果想得到进程的输出,管道是个很方便的方法,这样:

p=subprocess.Popen("dir", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdoutput,erroutput) = p.communicate()

p.communicate会一直等到进程退出,并将标准输出和标准错误输出返回,这样就可以得到子进程的输出了,上面,标准输出和标准错误输出是分开的,也可以合并起来,只需要将stderr参数设置为subprocess.STDOUT就可以了,这样子:

p=subprocess.Popen("dir", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
(stdoutput,erroutput) = p.communicate()

如果你想一行行处理子进程的输出,也没有问题:

p=subprocess.Popen("dir", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while True:
    buff = p.stdout.readline()
    if buff == '' and p.poll() != None:
        break

死锁

但是如果你使用了管道,而又不去处理管道的输出,那么小心点,如果子进程输出数据过多,死锁就会发生了,比如下面的用法:

p=subprocess.Popen("longprint", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
p.wait()

longprint是一个假想的有大量输出的进程,那么在我的xp, Python2.5的环境下,当输出达到4096时,死锁就发生了。当然,如果我们用p.stdout.readline或者p.communicate去清理输出,那么无论输出多少,死锁都是不会发生的。或者我们不使用管道,比如不做重定向,或者重定向到文件,也都是可以避免死锁的。

异步subprocess

无论是使用readline还是communicate,这里有个问题是:他们都是同步的,你没有办法在等待子进程输出的同时做点别的什么事情,标准的subprocess是不支持异步和子进程交互的,幸好,幸好,有人提供了Python 3下的异步方法,我移植到Python2.5下面,可以这样用了:

p=subprocess.Popen("dir", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while True:
    buff = p.asyncread(timeout=0.5)
    if buff == '' and p.poll() != None:
        break

这里带一个超时去读取子进程的输出,如果超时还没有输出,没关系,父进程可以干点别的什么事情,看起来很棒,subprocess的改动部分代码比较长,不在这里贴了。

关闭
在Python 2.6的subprocess模块中,新增加了一个小的接口就是Terminate,用于进程的终结,可惜可惜,十分遗憾,Windows下,这个Terminate只能杀死subprocess创建的进程,而不能杀死其子进程,如果我们明确知道创建的进程没有子进程,当然可以用这个接口,如果不肯定,则这个接口就没什么用了。

举个简单的例子,如果用shell=True的参数让subprocess创建进程,那么就会多出一个额外的cmd进程,这时用Terminate终结的就是这个cmd进程,而真正那个我们创建的进程则不会被终结。

有很多方法可以对付这个问题,但有一个简单的方法是使用Windows自己提供的taskkill命令,它有一个/T参数,可以杀死一个进程树,正是我们所需要的。subprocess创建的进程有一个pid属性,把pid传给taskkill就ok了。

http://www.liuzhongshu.com/code/subprocess-detail.html

Pyinstaller建立linux下Python的独立可执行文件

以下内容假定已安装好Python 2.4/2.5
一、下载并编译pyinstaller(只需做一次,以后可直接做第二步)
1.下载pyinstaller,现在的版本是1.3
(1)wget http://pyinstaller.hpcf.upr.edu/source/1.3/pyinstaller_1.3.tar.gz

2.解包进入源码目录
(1)tar zxv pyinstaller_1.3.tar.gz
(2)cd pyinstaller-1.3/source/linux

3.编译源代码
(1)python Make.py 生成python的 .pyc文件

如无错误,则出现如下提示(只有一行):
Now run "make" to build the targets: ../../support/loader/run ../../support/loader/run_d

(2)make 连接生成linux的 .o 文件

4.生成编译配置文件
(1)python Configure.py 生成config.dat配置文件

二、编译独立运行的python可执行文件

1.生成spec文件
python pyinstaller-1.3/Makespec.py --onefile --upx linuxlaptop.py

参数说明:
--onefile 生成单文件
--upx 生成压缩的文件(可减小执行文件体积,需先安装upx软件包)


2.生成最终的可执行文件
python pyinstaller-1.3/Build.py linuxlaptop.spec

执行完成后将在当前目录生成可执行的linuxlaptop文件,如有问题欢迎与我交流:linuxlaptop.cn。
原文:http://bbs.chinaunix.net/thread-1328460-1-1.html

============================个人实践=============================
系统:Ubuntu 8.0.4
安装相关包:apt-get install libc6-dev python-dev zlib1g-dev
cd source/linux
python ./Make.py
make


cd ../../
python Configure.py
/pyinstaller-1.3/Makespec.py --onedir --icon=img/imac.ico my.py
python /pyinstaller-1.3/Build.py my.spec


这时会提示pyinstaller-1.3中的几个包语法错误,估计源码包是在window下调试产生unix不识别的字符,典型的是换行符。
安装dos2unix:sudo apt-get install tofrodos
dos2unix /pyinstaller-1.3/pyinstaller-1.3/optparse.py......(6个文件左右)

再运行:python /pyinstaller-1.3/Build.py my.spec
搞定,生成了distmy目录,里头有好多.o文件,类似于win下的dll:)

Records:10‹12