[TOC]

代码示例支持

平台: 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

这些坑是新同学非常容易踩,而且 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 的坑_Python踩坑之旅其二裸用os.system的原罪相关推荐

  1. python shell下载很慢_Python踩坑之旅其一杀不死的Shell子进程

    1.1 踩坑案例 踩坑的程序是个常驻的Agent类管理进程, 包括但不限于如下类型的任务在执行:a. 多线程的网络通信包处理和控制Master节点交互 有固定Listen端口 b. 定期作业任务, 通 ...

  2. 使用Java读取 “Python写入redis” 的数据踩坑记录

    https://my.oschina.net/u/2338224/blog/3061507 使用Java读取 "Python写入redis" 的数据踩坑记录 https://seg ...

  3. python re零宽断言踩坑 re.error: look-behind requires fixed-width pattern

    python re零宽断言踩坑 在在线的正则校验工具上写了大半天才写出来的表达式,往python里一粘贴复制,代码行飘红,强行运行之后,报错. 表达式: #用单引号替换txt_line中匹配到的双引号 ...

  4. python导入类有红线_python踩坑系列之导入包时下划红线及报错“No module named”问题...

    python踩坑系列之导入包时下划红线及报错"No module named"问题 使用pycharm编写Python时,自己写了一个包(commontool),在同级另一个路径下 ...

  5. python代码下出现红线_python踩坑系列之导入包时下划红线及报错“No module named”问题...

    python踩坑系列之导入包时下划红线及报错"No module named"问题 使用pycharm编写Python时,自己写了一个包(commontool),在同级另一个路径下 ...

  6. python array赋值_从踩坑学Python内部原理(5):执行时机的差异

    (给Python开发者加星标,提升Python技能) 英文:Satwik Kansal,翻译:暮晨 Python开发者整理自 GitHub [导读]:Python 是一个设计优美的解释型高级语言,它提 ...

  7. python常见加密方式总结踩坑小贴士

    本文是向大家介绍python中常见的一些加密方式,在使用python的时候遇到数据加密的情况时,可以根据实际场景来选择加密的方式对数据进行加密,加强数据传输的安全性. 一.前言 日常工作中经常会看到各 ...

  8. 初次使用 python poetry 包管理模块踩坑

    注: 本文不讨论常规的 poetry 使用方法, 只讨论国内开发者在使用 poetry 时可能遇到的一系列问题, 并提供本人踩坑后的解决方法. poetry new, poetry init 在哪个目 ...

  9. python 扒数据_不踩坑的Python爬虫:如何在一个月内学会爬取大规模数据

    Python爬虫为什么受欢迎 如果你仔细观察,就不难发现,懂爬虫.学习爬虫的人越来越多,一方面,互联网可以获取的数据越来越多,另一方面,像 Python这样的编程语言提供越来越多的优秀工具,让爬虫变得 ...

  10. python调用imblearn中SMOTE踩坑

      SMOTE是用来解决样本种类不均衡,专门用来过采样化的一种方法.第一次接触,踩了一些坑,写这篇记录一下: 问题一:SMOTE包下载及调用 # 包下载 pip install imblearn# 调 ...

最新文章

  1. uboot环境变量-带分号的环境变量
  2. 沈阳生态所在保护性耕作促进农业可持续发展方面取得新进展
  3. @@ROWCOUNT 含义
  4. Java获取List泛型的真实类型
  5. nvm-windows的安装配置
  6. 【LeetCode笔记】958. 二叉树的完全性检验(Java、二叉树、BFS)
  7. 线段树、优先队列、单调队列小结
  8. 145元!苹果上架一块儿“天价抹布” ,你会买吗?
  9. AS出现Error:Cause: peer not authenticated
  10. [过年菜谱之]萝卜炖羊肉
  11. (转)C++类所占内存大小计算
  12. 时序图(Sequence Diagram)—UML图(六)
  13. 【语音分析】基于matlab倒谱分析与MFCC系数计算【含Matlab源码 556期】
  14. C语言怎么实现熊猫上香中的系统错误提示,熊猫烧香的病毒是用什么程序语言编写的 原理是什么...
  15. Java聊天室系统的设计与实现(完整源码 sql文件 论文)
  16. 为什么html中图片显示不出来,网页图片显示不出来是什么原因?
  17. Python——IDLE是什么意思?
  18. android 蓝牙Beacon开发
  19. vue 右键 单击 事件
  20. Android 手机连接电脑

热门文章

  1. Oracle merge into用法以及相关例子示例
  2. 一步步学习SPD2010--附录B--创建新的批准流程
  3. 二叉树非递归遍历算法(II)中序
  4. Spring Cloud Ribbon 的请求分发与原理
  5. @Resource真的只是按名称来进行依赖注入吗?@Autowired真的只是按照类型来依赖注入吗?
  6. Spring Cloud Sleuth服务链路跟踪之入门篇(学习总结)
  7. Java结构型设计模式之装饰者模式
  8. PHP依赖注入(DI)和控制反转(IOC)
  9. 1.Java集合-HashMap实现原理及源码分析
  10. Shell子程序结构,函数