码迷,mamicode.com
首页 > 编程语言 > 详细

python debug open_files

时间:2017-10-14 20:01:00      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:python   ini   har   bug   file   err   highlight   self   turn   

主要是遇到 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))

  

python debug open_files

标签:python   ini   har   bug   file   err   highlight   self   turn   

原文地址:http://www.cnblogs.com/Key-Ky/p/7668165.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!