python debug open_files

本文介绍了一种调试方法,用于定位并解决Error24: too many open files错误。通过重写Python内置的文件操作方法,可以追踪所有打开的文件,并提供了一个实用的命令来检查特定进程打开的文件情况。

主要是遇到 Error 24, too many open files.

下面这种方法可以debug打开了哪些文件。

import __builtin__
openfiles = set()
oldfile = __builtin__.file
class newfile(oldfile):
    def __init__(self, *args, **kwargs):
        self.x = args[0]
        print "### OPENING %s ###" % str(self.x)            
        oldfile.__init__(self, *args, **kwargs)
        openfiles.add(self)

    def close(self):
        print "### CLOSING %s ###" % str(self.x)
        oldfile.close(self)
        openfiles.remove(self)
oldopen = __builtin__.open
def newopen(*args):
    return newfile(*args, **kwargs)
__builtin__.file = newfile
__builtin__.open = newopen

def printOpenFiles():
    print "### %d OPEN FILES: [%s]" % (len(openfiles), ", ".join(f.x for f in openfiles))

可以使用 lsof -p 26530 | grep 'wav' >info.txt 查看进程26530的打开文件情况。

转载于:https://www.cnblogs.com/Key-Ky/p/7668165.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值