python_打印pstree效果
2013-11-22 11:20
357 查看
from subprocess import PIPE,Popen import shlex def pstree(): cmd = 'ps ax -o pid,ppid,command' sub = Popen(shlex.split(cmd),stdout=PIPE) return sub.stdout.readlines()[1:] def parse_ps(string): list_ps = [] for i in string: l = i.split() ps = {'pid':int(l[0]),'ppid':int(l[1]),'command':' '.join(l[2:])} list_ps.append(ps) return list_ps def show(pid,d,depth=3): show_root = [ i for i in d if i['pid'] == pid ][0] print '-'*depth,show_root['pid'],show_root['ppid'],show_root['command'] show_child = [ i for i in d if i['ppid'] == pid ] depth += 3 for i in show_child: show(i['pid'],d,depth) if __name__ == '__main__': show(1,parse_ps(pstree()),3)
相关文章推荐
- Python动态类型的学习---引用的理解
- 垃圾邮件过滤器 python简单实现
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- Python,Flex 2和Aptana[js开放工具]
- 国外开发者谈为何放弃PHP而改用Python
- 利用webqq协议使用python登录qq发消息源码参考
- python 判断自定义对象类型
- 让python的Cookie.py模块支持冒号做key的方法
- Python 面向对象 成员的访问约束
- 新手该如何学python怎么学好python?
- Python linecache.getline()读取文件中特定一行的脚本