代码示例支持 |
---|
平台: Centos 6.3 |
Python: 2.7.14 |
代码示例: 菜单 - Python踩坑指南代码示例 |
长期运行的daemon进程或者socket测试类进程, 常常遇到的坑是:shell
IOError: [Errno 24] Too many open files
bash
即进程遇到 IO 错误, 没法打开更多的文件.socket
通常从两个方面入手:测试
a. 谁打开谁关闭是个普适的原则:优化
# with 语法会在生命周期后自动关闭打开的文件 FD with open('xxxx_path.file', 'w') as fhandle: fhandle.dosth()
b. 检查文件 FD 是否存在泄漏设计
系统设计阶段通常会预估系统整体可打开的 FD 状况. 当出现以下状况时可能出现了泄漏 BUGcode
Python 基础库 CUP 提供对进程打开 FD 的支持, 详见示例代码.生命周期
以 Centos 6.3 Linux系统
为例, 查看 /etc/security/limits.conf 得到系统软硬限资源进程
* soft nofile 10240 * hard nofile 10240
其中, 用户不能突破系统的硬线 hard nofile limit
.
用户也能够经过 shell 命令 ulimit -n 来限定该 shell 启动的全部进程的 nofile
ulimit -a
能够查看当前用户被设定的限制, 示例:
[test@agent1 ~]$ ulimit -a core file size (blocks, -c) 0 ....... open files (-n) 10240 ..... virtual memory (kbytes, -v) unlimited file locks (-x) unlimited
Life is short. We use Python.