Python3 subprocess.Popen 调用次数太多出现OSError: [Errno 24] Too many open files

chensheng2611 2020-04-17 08:52:52

import os,time
from subprocess import *
from multiprocessing import *

def run_shell_cmd(cmd_str, index):
print('run shell cmd index %d'%(index,))
proc = Popen(['/bin/zsh', '-c', cmd_str],stdout=PIPE)
time.sleep(1)
outs = proc.stdout.readlines()
proc.stdout.close()
proc.terminate()
return

def multi_process_exc():
pool =
cmd_str = 'ps -ef | grep chromium'
for x in range(5000): #循环次数超过一定次数就会出现下面的问题
p = Process(target=run_shell_cmd, args=(cmd_str,x))
p.start()
pool.append(p)

for p in pool:
p.join()
if __name__ == "__main__":
multi_process_exc()

运行结果如下
Process Process-4078:
Traceback (most recent call last):
File "/usr/lib/python3.8/multiprocessing/process.py", line 315, in _bootstrap
self.run()
File "/usr/lib/python3.8/multiprocessing/process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "test.py", line 8, in run_shell_cmd
proc = Popen(['/bin/zsh', '-c', cmd_str], stdout=PIPE)
File "/usr/lib/python3.8/subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib/python3.8/subprocess.py", line 1603, in _execute_child
errpipe_read, errpipe_write = os.pipe()
OSError: [Errno 24] Too many open files


系统环境:
Linux DJ 5.4.28-1-MANJARO #1 SMP PREEMPT Wed Mar 25 12:45:29 UTC 2020 x86_64 GNU/Linux
Python3.8
...全文
2330 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Binary_8 2023-01-16
  • 打赏
  • 举报
回复

请问解决了吗?这个问题

chensheng2611 2020-04-17
  • 打赏
  • 举报
回复
自己顶一下,实际环境中就是子进程会执行shell命令读取当前所有进程。 进程开的过多的话就超过系统限制,lsof查看有大量PIPE未关闭,试过各种方法也关闭不了,不知哪位大神帮忙解答下
xxxtentacition 2021-07-28
  • 举报
回复
@chensheng2611 请问解决了吗

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧