python open文件被另一个进程打开怎么办_Python太多的打开文件(子进程)

I seem to be having an issue with Python when I run a script that creates a large number of sub processes. The sub process creation code looks similar to:

Code:

def execute(cmd, stdout=None, stderr=subprocess.STDOUT, cwd=None):

proc = subprocess.Popen(cmd, shell=True, stdout=stdout, stderr=stderr, cwd=cwd)

atexit.register(lambda: __kill_proc(proc))

return proc

The error message I am receiving is:

OSError: [Errno 24] Too many open files

Once this error occurs, I am unable to create any further sub processes until kill the script and start it again. I am wondering if the following line could be responsible.

atexit.register(lambda: __kill_proc(proc))

Could it be that this line creates a reference to the sub process, resulting in a "file" remaining open until the script exits?

解决方案

So it seems that the line:

atexit.register(lambda: __kill_proc(proc))

was indeed the culprit. This is probably because of the Popen reference being kept around so the process resources aren't free'd. When I removed that line the error went away. I have now changed the code as @Bakuriu suggested and am using the process' pid value rather than the Popen instance.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值