Python踩坑之旅其二裸用os.system的原罪
代码示例支持 |
---|
平台: Centos 6.3 |
Python: 2.7.14 |
Github: https://github.com/baidu/CUP |
1.1 踩坑案例
今天的坑不仅包括裸用os.system还包括裸用相关的家族:
- os.popen
- subprocess家族
- subprocess.call
- subprocess.Popen
- subprocess.run
- commands家族 (py2.6后已不推荐使用, depreciated. Py3删除)
- commands.getstatusoutput
- subprocess家族
这些坑是新同学非常容易踩,而且 code review 过程中容易漏掉:
[1] 长期运行 Service 中裸用以函数家族
- 裸用以上 shell 执行家族可能出现script 执行 hang 住进而 hang 住逻辑执行线程,长时间积累进而占满所有执行线程而服务宕机情况
- 大内存消耗 service fork 子进程直接执行 script
- 如果该 script hang 住
- 并且原进程内存进行频繁修改(或者堆积修改, 虽然有 Copy-On-Write技术),但由于内存巨大,依然有内存风险
[2] 自动化测试中裸用以上函数家族而不加以保护
- 单个 case 如果执行 script 脚本 hang 住会导致 hang 掉整个case 集
- 不设计 case 超时机制导致case 集合运行时间不可控
1.2 填坑解法
- 支持超时 kill 策略,禁止任何情况下的 shell 执行裸用家族函数
提供一个作者的代码参考: https://github.com/baidu/CUP/blob/master/cup/shell/oper.py
from cup import shell
shellexec = shell.ShellExec()
# timeout=None will block the execution until it finishes
shellexec.run('/bin/ls', timeout=None)
# timeout>=0 will open non-blocking mode
# The process will be killed if the cmd timeouts
shellexec.run(cmd='/bin/ls', timeout=100)
见ShellExec类的run函数
- 内存消耗型服务/进程, 长期运行服务进程避免fork 进程执行 shell 命令
1.3 坑位分析
建议看下第二章节关于进程和子进程继承类信息,script使用上述家族进行执行时,采用了启动一个子进程的方式
1.4.1 技术关键字
- os.system家族
- subprocess家族
1.5 填坑总结
Shell执行是个非常常见的操作,所以很多同学特别是新同学,在使用过程中经常不注意而随意使用。 裸用一时爽,进程死亡火葬场
2. 前坑回顾
2.1 Linux中, 子进程拷贝父进程哪些信息
- 先说与父进程不同的
- pid, ppid
- memory locks
- tms_utime、tms_stime、tms_cutime、tms_ustime
- pending signals
- semaphore adjustments
- file lock
- pending alarms
参考资料来源:
- Linux Programmer's Manual (
man fork
)- CentOS release 6.3 (Final)
- Linux Kernel 2.6.32
fork() creates a new process by duplicating the calling process. The new process, referred to as the child, is an exact duplicate of the calling process, referred to as the parent, except for the follow-
ing points:
* The child has its own unique process ID, and this PID does not match the ID of any existing process group (setpgid(2)).
* The child's parent process ID is the same as the parent's process ID.
* The child does not inherit its parent's memory locks (mlock(2), mlockall(2)).
* Process resource utilizations (getrusage(2)) and CPU time counters (times(2)) are reset to zero in the child.
* The child's set of pending signals is initially empty (sigpending(2)).
* The child does not inherit semaphore adjustments from its parent (semop(2)).
* The child does not inherit record locks from its parent (fcntl(2)).
* The child does not inherit timers from its parent (setitimer(2), alarm(2), timer_create(2)).
* The child does not inherit outstanding asynchronous I/O operations from its parent (aio_read(3), aio_write(3)), nor does it inherit any asynchronous I/O contexts from its parent (seeio_setup(2)).
The process attributes in the preceding list are all specified in POSIX.1-2001. The parent and child also differ with respect to the following Linux-specific process attributes:
* The child does not inherit directory change notifications (dnotify) from its parent (see the description of F_NOTIFY in fcntl(2)).
* The prctl(2) PR_SET_PDEATHSIG setting is reset so that the child does not receive a signal when its parent terminates.
* Memory mappings that have been marked with the madvise(2) MADV_DONTFORK flag are not inherited across a fork().
* The termination signal of the child is always SIGCHLD (see clone(2)).
在说继承、拷贝父进程的
- 包括
- 内部数据空间
- 堆栈
- 用户 ID、组 ID、eid 有效用户 id、有效组 id、用户 id 标志和设置组 id 标志
- 进程组 id
- 会话 id
- 终端
- 当前目录、根目录
- 文件模式屏蔽字
- 信号屏蔽设置
- 打开文件描述符
- 环境
- 共享存储段
- 存储映射
- 资源限制
此外
- 在父进程创建 (fork) 出一个子进程过程中, 为了加速, 会使用叫做 copy-on-write 的技术.
- 这项技术在存储软件领域也经常使用
- 附上一个关于它的讨论,点击查看
2.2 Agent常驻进程选择>60000端口的意义
在 Linux 系统中, 一般系统会自动替程序选择端口连接到用户指定的目的端口, 而这个端口范围是提前设定好的, 比如作者的 centos:
$ cat /proc/sys/net/ipv4/ip_local_port_range
10000 60000
- 选择 60000 以上的端口可以避免冲突
- 附上一篇讨论该ip_local_port_range的文章,欢迎查看
Python踩坑之旅其二裸用os.system的原罪的更多相关文章
- Python踩坑之旅其一杀不死的Shell子进程
目录 1.1 踩坑案例 1.2 填坑解法 1.3 坑位分析 1.4 坑后扩展 1.4.1 扩展知识 1.4.1 技术关键字 1.5 填坑总结 1.1 踩坑案例 踩坑的程序是个常驻的Agent类管理进程 ...
- Python 踩坑之旅进程篇其三pgid是个什么鬼 (子进程\子孙进程无法kill 退出的解法)
目录 1.1 踩坑案例 1.2 填坑解法 1.3 坑位分析 1.4.1 技术关键字 下期坑位预告 代码示例支持 平台: Centos 6.3 Python: 2.7.14 Github: https: ...
- [代码修订版] Python 踩坑之旅 [进程篇其四] 踩透 uid euid suid gid egid sgid的坑坑洼洼
目录 1.1 踩坑案例 1.2 填坑解法 1.3 坑位分析 1.4 技术关键字 1.5 坑后思考 下期坑位预告 代码示例支持 平台: Centos 6.3 Python: 2.7.14 代码示例: 公 ...
- Python 踩坑之旅进程篇其四一次性踩透 uid euid suid gid egid sgid的坑坑洼洼
目录 1.1 踩坑案例 1.2 填坑解法 1.3 坑位分析 1.4 技术关键字 1.5 坑后思考 下期坑位预告 代码示例支持 平台: Centos 6.3 Python: 2.7.14 代码示例: 菜 ...
- [代码修订版] Python 踩坑之旅进程篇其五打不开的文件
目录 1.1 踩坑案例 1.2 填坑和分析 1.2.1 从程序优化入手 1.2.2 从资源软硬限入手 1.4.1 技术关键字 下期坑位预告 代码示例支持 平台: Centos 6.3 Python: ...
- Python 踩坑之旅文件系统篇其一文件夹也是个文件
目录 1.1 案例 1.2 分析 1.3 扩展 1.4 技术关键字 下期预告 代码示例支持 平台: Mac OS Python: 2.7.10 代码示例: - wx: 菜单 - Python踩坑指南代 ...
- EasyTrader踩坑之旅总结
easytrader是用python写的可以调用主要券商完成自动化炒股的一个软件 ,但我用的是同花顺,在研究过程中,发现同花顺暂时调不通.后来搜索发现thstrade的源码作者说是easytrad ...
- 我的微信小程序入门踩坑之旅
前言 更好的阅读体验请:我的微信小程序入门踩坑之旅 小程序出来也有一段日子了,刚出来时也留意了一下.不过赶上生病,加上公司里也有别的事,主要是自己犯懒,就一直没做.这星期一,赶紧趁着这股热乎劲,也不是 ...
- vue+ vue-router + webpack 踩坑之旅
说是踩坑之旅 其实是最近在思考一些问题 然后想实现方案的时候,就慢慢的查到这些方案 老司机可以忽略下面的内容了 1)起因 考虑到数据分离的问题 因为server是express搭的 自然少 ...
随机推荐
- ubuntu 安装cuda 成功
洗洗睡了
- HihoCoder1670 : 比赛日程安排([Offer收割]编程练习赛41)(模拟)
描述 H国编程联赛中有N只队伍,编号1~N. 他们计划在2018年一共进行M场一(队)对一(队)的比赛. 为了让参赛队员能得到充分的休息,联赛组委会决定:每支队伍连续两场比赛之间至少间隔一天.也就是如 ...
- BZOJ1855 [Scoi2010]股票交易[单调队列dp]
题 题面有点复杂,不概括了. 后面的状态有前面的最优解获得大致方向是dp.先是瞎想了个$f[i][j]$表示第$i$天手里有$j$张股票时最大收入(当天无所谓买不买). 然后写了一个$O(n^4)$状 ...
- SVN版本控制详解
1 版本控制 1.1 如果没有版本控制? Team开发必备. 一个人开发(必备). 版本控制:控制(代码)版本. 论文:版本控制? 毕业论文-4-22.doc 毕业论文-5-01.doc 毕业论文-f ...
- select查询语句执行顺序
查询中用到的关键词主要包含六个,并且他们的顺序依次为select--from--where--group by--having--order by其中select和from是必须的,其他关键词是可选的 ...
- 单机11g ogg 双向DML复制
环境说明: Linux为Linux 2.6.32-573.el6.x86_64 Oracle为 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Pr ...
- poj3422K方格取数——最大费用最大流
题目:http://poj.org/problem?id=3422 最大费用最大流: 拆点,在自点之间连两条边,一条容量为1,边权为数字:一条容量为k-1,边权为0:表示可以走k次,只有一次能取到数字 ...
- 搭建基于Nagios的监控系统——之监控远程Windows服务器
分享了如何监控Linux服务器,我们来看看使用Nagios如何监控Windows服务器. 第一部分:配置被监控的Windows服务器 首先,访问 http://sourceforge.net/pr ...
- iview组件DatePicker type="datetimerange绑定初始默认时间值
使用::value="[this.startTime,this.endTime]",绑定当天时间 如下: <DatePicker type="datetimeran ...
- 利用mysql客户端查询UCSC数据库
UCSC Genome Browser是由University of California Santa Cruz (UCSC) 创立和维护的,该站点包含有人类.小鼠和大鼠等多个物种的基因组草图和注释信 ...